{"id":16293,"date":"2016-11-07T14:00:28","date_gmt":"2016-11-07T19:00:28","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16293"},"modified":"2016-11-07T14:00:28","modified_gmt":"2016-11-07T19:00:28","slug":"objective-c-id-as-swift-any","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/11\/07\/objective-c-id-as-swift-any\/","title":{"rendered":"Objective-C id as Swift Any"},"content":{"rendered":"<p><a href=\"https:\/\/developer.apple.com\/swift\/blog\/?id=39\">Apple<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/swift\/blog\/?id=39\">\n<p>In Swift 3, the <code>id<\/code> type in Objective-C now maps to the <code>Any<\/code> type in Swift, which describes a value of any type, whether a class, enum, struct, or any other Swift type. This change makes Objective-C APIs more flexible in Swift, because Swift-defined value types can be passed to Objective-C APIs and extracted as Swift types, eliminating the need for manual &ldquo;box&rdquo; types. These benefits also extend to collections: Objective-C collection types <code>NSArray<\/code>, <code>NSDictionary<\/code>, and <code>NSSet<\/code>, which previously only accepted elements of <code>AnyObject<\/code>, now can hold elements of <code>Any<\/code> type. For hashed containers, such as <code>Dictionary<\/code> and <code>Set<\/code>, there&rsquo;s a new type <code>AnyHashable<\/code> that can hold a value of any type conforming to the Swift <code>Hashable<\/code> protocol.<\/p>\n<p>[&#8230;]<\/p>\n<p>Property lists, JSON, and user info dictionaries are common in Cocoa, and Cocoa natively represents these as untyped collections. In Swift 2, it was necessary to build <code>Array<\/code>, <code>Dictionary<\/code>, or <code>Set<\/code> with <code>AnyObject<\/code> or <code>NSObject<\/code> elements for this purpose, relying on implicit bridging conversions to handle value types[&#8230;] Swift now imports Cocoa APIs as accepting collections of <code>Any<\/code> and\/or <code>AnyHashable<\/code>, so we can change the collection type to use <code>[AnyHashable: Any]<\/code> instead of <code>[NSObject: AnyObject]<\/code> or <code>NSDictionary<\/code>, without changing any other code.<\/p>\n<p>[&#8230;]<\/p>\n<p><code>Any<\/code> does not have the same magic method lookup behavior as <code>AnyObject<\/code>. This may break some Swift 2 code that looked up a property or sent a message to an untyped Objective-C object.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Apple: In Swift 3, the id type in Objective-C now maps to the Any type in Swift, which describes a value of any type, whether a class, enum, struct, or any other Swift type. This change makes Objective-C APIs more flexible in Swift, because Swift-defined value types can be passed to Objective-C APIs and extracted [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[46,54,71,901],"class_list":["post-16293","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=16293"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16293\/revisions"}],"predecessor-version":[{"id":16294,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16293\/revisions\/16294"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}