{"id":44086,"date":"2024-07-15T13:43:53","date_gmt":"2024-07-15T17:43:53","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=44086"},"modified":"2024-07-15T13:43:53","modified_gmt":"2024-07-15T17:43:53","slug":"nscopying-in-a-swift-world","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2024\/07\/15\/nscopying-in-a-swift-world\/","title":{"rendered":"NSCopying in a Swift World"},"content":{"rendered":"<p><a href=\"https:\/\/pspdfkit.com\/blog\/2024\/nscopying-in-a-swift-world\/\">Douglas Hill<\/a>:<\/p>\n<blockquote cite=\"https:\/\/pspdfkit.com\/blog\/2024\/nscopying-in-a-swift-world\/\">\n<p>This crash happens because, behind the scenes, the Swift compiler synthesises overrides of a superclass&rsquo;s designated initialisers. These overridden initialisers crash to prevent objects from being incorrectly initialised from Objective-C.<\/p>\n<p>[&#8230;]<\/p>\n<p>From a quick look on Stack Overflow, it seems <code>[self.class alloc]<\/code> is often a recommended way to create a copy in Objective-C. However, the problem is that the use of <code>self.class<\/code> dynamically looks up the subclass <code>SocialDocument<\/code>, but the code here in our framework has no idea that <code>SocialDocument<\/code> has changed the initialisation requirements.<\/p>\n<p>[&#8230;]<\/p>\n<p>If <code>Document<\/code> were a simpler type where all state that should be copied was public, then subclasses that required copying to create instances of the subclass could override <code>copy(with:)<\/code> without calling <code>super<\/code>[&#8230;]<\/p>\n<p>[&#8230;]<\/p>\n<p>There isn&rsquo;t a nice way to make copying subclasses work while still adhering to Swift&rsquo;s principle of reducing the amount of mutable state by using <code>let<\/code> to create read-only properties.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/x.com\/akashivskyy\/status\/1393236621106028549\">Adrian Kashivskyy<\/a>:<\/p>\n<blockquote cite=\"https:\/\/x.com\/akashivskyy\/status\/1393236621106028549\"><p>PSA: Don&rsquo;t cast values to NSCopying in Swift or you risk a <a href=\"https:\/\/github.com\/swiftlang\/swift\/issues\/56987\">crash<\/a> at runtime. Learned this the hard way. &#x1F920;<\/p><p>[&#8230;]<\/p><p>Because all values that were originally bridged from Objective-C will pass the alone <code>NSCopying<\/code> cast, even when they don&rsquo;t actually conform to the protocol. Such values immediately become instances of <code>_SwiftValue<\/code> and, like trojans, they will sit there\npretending to be innocent but will crash at runtime as soon as they&rsquo;re accessed.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2023\/06\/16\/swift-proposal-init-accessors\/\">Swift Proposal: Init Accessors<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/05\/22\/marking-unused-required-swift-initializers-as-unavailable\/\">Marking Unused Required Swift Initializers As Unavailable<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/08\/31\/using-lazy-variables-to-work-around-swift-initialization-rules\/\">Using Lazy Variables to Work Around Swift Initialization Rules<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/08\/03\/required-swift-initializers-and-decodable\/\">&ldquo;required&rdquo; Swift Initializers and Decodable<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/11\/17\/mixing-swift-initializers\/\">Mixing Swift Initializers<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/11\/30\/swift-init\/\">Swift init()<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2015\/05\/10\/failable-initializers-revisited\/\">Failable Initializers, Revisited<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2014\/11\/25\/strange-tales-of-swift-initialization\/\">Strange Tales of Swift Initialization<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Douglas Hill: This crash happens because, behind the scenes, the Swift compiler synthesises overrides of a superclass&rsquo;s designated initialisers. These overridden initialisers crash to prevent objects from being incorrectly initialised from Objective-C. [&#8230;] From a quick look on Stack Overflow, it seems [self.class alloc] is often a recommended way to create a copy in Objective-C. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2024-07-15T17:43:58Z","apple_news_api_id":"6d194111-3b01-442c-a045-c10803e605cc","apple_news_api_modified_at":"2024-07-15T17:43:58Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AbRlBETsBRCygRcEIA-YFzA","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":[69,31,2321,30,2385,54,71,1424,901],"class_list":["post-44086","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-ios-17","tag-mac","tag-macos-14-sonoma","tag-objective-c","tag-programming","tag-pspdfkit","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44086","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=44086"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44086\/revisions"}],"predecessor-version":[{"id":44087,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/44086\/revisions\/44087"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=44086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=44086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=44086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}