{"id":18986,"date":"2017-09-25T15:05:32","date_gmt":"2017-09-25T19:05:32","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18986"},"modified":"2017-09-25T15:05:32","modified_gmt":"2017-09-25T19:05:32","slug":"surprising-behavior-of-non-optional-nsmanaged-properties","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/09\/25\/surprising-behavior-of-non-optional-nsmanaged-properties\/","title":{"rendered":"Surprising Behavior of Non-optional @NSManaged Properties"},"content":{"rendered":"<p><a href=\"http:\/\/holko.pl\/2017\/09\/18\/surprising-non-optional-nsmanaged\/\">Arek Holko<\/a>:<\/p>\n<blockquote cite=\"http:\/\/holko.pl\/2017\/09\/18\/surprising-non-optional-nsmanaged\/\">\n<p>A few minutes (or <em>more honestly<\/em> hours) of debugging later, we notice that the <code>name<\/code> is an empty string only on instances that were deleted from a context and the context was saved. (Deletion of managed objects is unrelated to ARC, so we have objects living in memory even though they are already treated as deleted.)<\/p>\n<p>[&#8230;]<\/p>\n<p>As <a href=\"https:\/\/www.reddit.com\/r\/iOSProgramming\/comments\/70vp58\/surprising_behavior_of_nonoptional_nsmanaged\/dn7n99s\/\">pointed out on Reddit<\/a> replacement of <code>nil<\/code> with an empty string is actually caused by <a href=\"https:\/\/github.com\/apple\/swift\/blob\/5c4fe26aba4cc0fc19a8cc811a4f3d30cbd4cea7\/stdlib\/public\/SDK\/Foundation\/String.swift#L53-L60\">bridging from <code>NSString<\/code> to <code>String<\/code><\/a>.)<\/p>\n<p>[&#8230;]<\/p>\n<p>The same silent substitution happens for these types too:<\/p>\n<ul>\n<li>numeric types use a value equal to <code>0<\/code><\/li>\n<li><code>Data<\/code> uses an empty <code>Data<\/code> instance<\/li>\n<\/ul>\n<\/blockquote>\n\n<p>So this does not seem to be Core Data&#x2013;specific. However:<\/p>\n\n<blockquote cite=\"http:\/\/holko.pl\/2017\/09\/18\/surprising-non-optional-nsmanaged\/\"><p>This behavior spans relationships too. [&#8230;] Can you guess what happens? We didn&rsquo;t set up the  <code>user<\/code> relationship, so we should get either a <code>nil<\/code> or a fatal error, right? Well, we don&rsquo;t. An empty string is printed in this case too!<\/p>\n<p>This is completely counter-intuitive when compared to the normal Swift code: <code>event.user<\/code> is <code>nil<\/code>, yet <code>event.user.name<\/code> isn&rsquo;t.<\/p>\n<\/blockquote>\n\n<p>Here, <code>event.user<\/code> was declared as non-optional.<\/p>","protected":false},"excerpt":{"rendered":"<p>Arek Holko: A few minutes (or more honestly hours) of debugging later, we notice that the name is an empty string only on instances that were deleted from a context and the context was saved. (Deletion of managed objects is unrelated to ARC, so we have objects living in memory even though they are already [&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":[69,109,31,1472,30,1529,71,901],"class_list":["post-18986","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-coredata","tag-ios","tag-ios-11","tag-mac","tag-macos-10-13","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18986","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=18986"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18986\/revisions"}],"predecessor-version":[{"id":18987,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18986\/revisions\/18987"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18986"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18986"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18986"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}