3 ms·
$ swift Welcome to Apple Swift version 5.3 (swiftlang-1200.0.16.15 clang-1200.0.22.41). Type :help for assistance. 1> import Foundation 2> // IIRC t
by saagarjha 6y ago
$ swift
Welcome to Apple Swift version 5.3 (swiftlang-1200.0.16.15 clang-1200.0.22.41).
Type :help for assistance.
1> import Foundation
2> // IIRC this used to throw an exception.
3> // Now a respondsToSelector: check is silently inserted and we get a surprise nil.
4> (Int?.none as AnyObject).count + "call Swift's bluff".count
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file repl.swift, line 4
2020-07-10 18:03:43.815959-0700 repl_swift[73029:930570] Fatal error: Unexpectedly found nil while unwrapping an Optional value: file repl.swift, line 4
Execution interrupted. Enter code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)
- babesh 6y agoYou need to call Swift's bluff with AnyObject. That's unsafe and was only necessary to bridge to Objective-C where you can call any method on any object. I think we are in general agreement?
- saagarjha 6y agoWhen you write an iOS app you're constantly interacting with Objective-C. The moment you're using JSONSerialization–and unless you're shipping your own JSON parser, you're not avoiding it–you're getting back a nice Any that you are almost certainly going to work with, possibly implicitly, using AnyObject. My point was that switching the SDK to use Swift would not help fix this issue, and I think I've shown that you'd run into similar issues even without the use of the force unwrap operator as you implied previously.
- babesh 6y agoFair enough.