Lecture 5 Objective-C Compatibility

Objective-C Compatibility

Bridging

  • Virtually all of the iOS API is accessible seamlessly from Swift.
  • A few special data types are handled specially and powerfully via bridging.
  • Bridging means that you can use them interchangeably.
  • NSString is bridged to String.
  • NSArray is bridged to Array<AnyObject>.
  • NSDictionary is bridged to Dictionary<NSObject, AnyObject>.
  • Int, Float, Double, Bool are all bridged to NSNumber (but not vice-versa).
  • To get from NSNumber to these types use doubleValue, intValue, etc.
  • Int, Float, Double are bridged to the C types int, float, double.

Casting to/from bridged types

  • Cast to/from a bridged type, no need for as?

  • String, Array and Dictionary are structs, not objects.

  • But they can still be an AnyObject, because they are bridged to these NS versions which are objects.

  • The keys in an NSDictionary are NSObject subclasses (Dictionary<NSObject, AnyObject>). But that does not prevent a Swift String from being a key in an NSDictionary, because it is bridged to NSString which does inherit from NSObject.

Property List

  • Property List means an AnyObject which is known to be a collection of objects which are ONLY on of NSString, NSArray, NSDictionary, NSNumber, NSData, NSDate.
  • Property Lists are used to pass around data blindly.
  • The semantics of the contents of a Property List are known only to its creator.
  • Property Lists are also used as a __generic data structure.
  • And so can be passes to API that reads/writes generic data.

NSUserDefaults

  • It’s essentially a very tiny database that stores Property List data.
  • It persists between launchings of your application.
  • Great for things like “settings” and such.
  • Do NOT use it for anything big

Views

Leave a Reply

Your email address will not be published. Required fields are marked *