{"id":14242,"date":"2016-04-21T10:24:28","date_gmt":"2016-04-21T14:24:28","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=14242"},"modified":"2016-04-21T10:24:28","modified_gmt":"2016-04-21T14:24:28","slug":"core-data-type-safety-with-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/04\/21\/core-data-type-safety-with-swift\/","title":{"rendered":"Core Data Type Safety With Swift"},"content":{"rendered":"<p><a href=\"https:\/\/www.raizlabs.com\/dev\/2016\/04\/core-data-type-safety-with-swift\/\">Brian King<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.raizlabs.com\/dev\/2016\/04\/core-data-type-safety-with-swift\/\">\n<p>Xcode also supports Swift code generation, but I don&rsquo;t think developers should use it. First off, the amount of code you have to write to use Core Data with Swift is less than with Objective-C, since there are not separate interface and implementation files and the property syntax is simpler. It&rsquo;s not that hard to do by hand, as we&rsquo;ll see. Second, types are so much more important in Swift, and <code>NSManagedObject<\/code> is actually incredibly smart when it comes to types and Swift.<\/p>\n<p>[&#8230;]<\/p>\n<p>By default, to-many relationships will be generated as\n<code>NSSet?<\/code>. But who can deal with typeless containers these days? Use the native Swift\n<code>Set<\/code> type instead.<\/p>\n<p>[&#8230;]<\/p>\n<p>If you&rsquo;re using Core Data with Swift, I hope this inspires you to revisit you models and reduce the amount of force casting and optional unwrapping in your code.<\/p>\n<\/blockquote>\n<p>In <a href=\"https:\/\/developer.apple.com\/library\/mac\/releasenotes\/DeveloperTools\/RN-Xcode\/Chapters\/xc7_release_notes.html\">Xcode 7.3<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/library\/mac\/releasenotes\/DeveloperTools\/RN-Xcode\/Chapters\/xc7_release_notes.html\"><p>The <code>NSManaged<\/code> attribute can be used with methods as well as properties, for access to Core Data&rsquo;s automatically generated Key-Value-Coding-compliant to-many accessors.<\/p>\n<pre>@NSManaged var employees: NSSet\n\n@NSManaged func addEmployeesObject(employee: Employee)\n@NSManaged func removeEmployeesObject(employee: Employee)\n@NSManaged func addEmployees(employees: NSSet)\n@NSManaged func removeEmployees(employees: NSSet)\n<\/pre>\n<p>These can be declared in your <code>NSManagedObject<\/code> subclass.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Brian King: Xcode also supports Swift code generation, but I don&rsquo;t think developers should use it. First off, the amount of code you have to write to use Core Data with Swift is less than with Objective-C, since there are not separate interface and implementation files and the property syntax is simpler. It&rsquo;s not that [&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":[109,31,1137,1245,30,1199,1004,71,901],"class_list":["post-14242","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-coredata","tag-ios","tag-ios-9","tag-key-value-coding-kvc","tag-mac","tag-mac-os-x-10-11","tag-mogenerator","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14242","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=14242"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14242\/revisions"}],"predecessor-version":[{"id":14243,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14242\/revisions\/14243"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=14242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=14242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=14242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}