{"id":10840,"date":"2015-03-17T00:12:35","date_gmt":"2015-03-17T04:12:35","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10840"},"modified":"2015-03-17T00:12:35","modified_gmt":"2015-03-17T04:12:35","slug":"using-core-data-with-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/03\/17\/using-core-data-with-swift\/","title":{"rendered":"Using Core Data With Swift"},"content":{"rendered":"<p><a href=\"http:\/\/www.atomicbird.com\/blog\/swift-core-data\">Tom Harrington<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.atomicbird.com\/blog\/swift-core-data\"><p>Both Core Data and Swift include the concept of an optional value. But when Xcode generates subclasses, it doesn&rsquo;t consider whether Core Data thinks the value is optional when generating the Swift file. It generates non-optional properties every time. This conflict means that you end up a Swift non-optional property that could reasonably have a nil value. The compiler sees that the property is not optional, so it doesn&rsquo;t mind that you aren&rsquo;t checking this (in fact, Swift rules mean it&rsquo;s not possible to check, because the property is not optional). This is exactly the kind of problem Swift&rsquo;s compiler is supposed to catch for you, but here it can&rsquo;t.<\/p><\/blockquote>\n<p>This can lead to crashes.<\/p>\n<blockquote cite=\"http:\/\/www.atomicbird.com\/blog\/swift-core-data\"><p>If you&rsquo;re using mogenerator, you&rsquo;re covered for Core Data optionals. It makes sure Core Data optionals become Swift optionals. I&rsquo;d take it a step farther and make all properties optional in Swift even if they&rsquo;re required by Core Data. Core Data doesn&rsquo;t enforce the &ldquo;required&rdquo; rule until you save changes, so even non-optional attributes can legally be nil at almost any time.<\/p><p>[&#8230;]<\/p><p>Although the documentation could be read as meaning that <code>@NSManaged<\/code> is required, it actually isn&rsquo;t. It&rsquo;s only needed if you&rsquo;ll be letting Core Data handle the accessors. If you&rsquo;re providing your own, drop it. Core Data&rsquo;s accessor magic is not documented but it seems you can&rsquo;t just override it like you&rsquo;d override other methods.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Tom Harrington: Both Core Data and Swift include the concept of an optional value. But when Xcode generates subclasses, it doesn&rsquo;t consider whether Core Data thinks the value is optional when generating the Swift file. It generates non-optional properties every time. This conflict means that you end up a Swift non-optional property that could reasonably [&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":[131,69,109,31,30,71,901],"class_list":["post-10840","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-bug","tag-cocoa","tag-coredata","tag-ios","tag-mac","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10840","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=10840"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10840\/revisions"}],"predecessor-version":[{"id":10841,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10840\/revisions\/10841"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}