{"id":9235,"date":"2022-03-06T23:36:00","date_gmt":"2022-03-06T23:36:00","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=9235"},"modified":"2022-03-06T23:36:00","modified_gmt":"2022-03-06T23:36:00","slug":"init","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/init\/","title":{"rendered":"init?"},"content":{"rendered":"<p>I&#8217;d been wondering why, when creating some types in Swift I was seeing a requirement to unwrap the type. After all, surely if I created a types it&#8217;s not nil and hence why does it need to be optional?<\/p>\n<p>Well Swift does actually allow us to declare a type&#8217;s init as optional, for example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nstruct Email {\r\n   var address: String\r\n\r\n   init?(address: String) {\r\n      \/\/ not really much of a validation step\r\n      \/\/ but in the real world we could ensure\r\n      \/\/ the address matches an email regex\r\n      guard address.count &gt; 0 else {\r\n         return nil\r\n     }\r\n   }\r\n}\r\n<\/pre>\n<p>This is pretty useful as it means if we initialize a type with invalid values, we can simple return a nil optional object instead of the alternative of throwing an exception or the need for a two phase initialization. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;d been wondering why, when creating some types in Swift I was seeing a requirement to unwrap the type. After all, surely if I created a types it&#8217;s not nil and hence why does it need to be optional? Well Swift does actually allow us to declare a type&#8217;s init as optional, for example struct [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[286],"tags":[],"class_list":["post-9235","post","type-post","status-publish","format-standard","hentry","category-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=9235"}],"version-history":[{"count":1,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9235\/revisions"}],"predecessor-version":[{"id":9236,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/9235\/revisions\/9236"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=9235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=9235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=9235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}