{"id":22326,"date":"2018-07-31T16:26:48","date_gmt":"2018-07-31T20:26:48","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=22326"},"modified":"2018-08-02T12:48:03","modified_gmt":"2018-08-02T16:48:03","slug":"objective-c-features-that-i-wish-existed","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2018\/07\/31\/objective-c-features-that-i-wish-existed\/","title":{"rendered":"Objective-C Features That I Wish Existed"},"content":{"rendered":"<p><a href=\"http:\/\/dobegin.com\/objc-features-wishlist\/\">Daniel Lazarenko<\/a> (via <a href=\"https:\/\/twitter.com\/self_vs_this\/status\/1022947906737922048\">Gianluca Bertani<\/a>):<\/p>\n<blockquote cite=\"http:\/\/dobegin.com\/objc-features-wishlist\/\"><p>The features listed here are chosen to have a relatively small scope in order to be implemented without major changes and in a backwards compatible way in the spirit of Objective-C. Thus they are not meant to turn Objective-C into a modern experimental language like Swift, but should make programming experience better and reduce boilerplate.<\/p>\n<p>[&#8230;]<\/p>\n<p>In 2015 Xcode 6.3 introduced nullability annotations to Objective-C. With those you can express an intent that a pointer to an object can never be <code>nil<\/code>. Unfortunately the only observable check that the Objective-C compiler does with that is that it prevents you from passing the <code>nil<\/code> constant in places where a <code>nonnull<\/code> type is expected. If you pass in a <code>nullable<\/code> pointer variable (not a <code>nil<\/code> constant), this is silently allowed[&#8230;]<\/p><p>[&#8230;]<\/p>\n<p>Imagine a piece of code that uses a lot of blocks, including nested blocks where on completion you want to do something else asynchronously, so you have to make a second <code>strongSelf<\/code>. This pattern becomes boilerplate code! It is possible to shrink the lines by using <a href=\"https:\/\/stackoverflow.com\/questions\/28305356\/ios-proper-use-of-weakifyself-and-strongifyself\"><code>@weakify\/@strongify<\/code><\/a> custom helper macros, but you still have to write them and have them available in your project.<\/p><\/blockquote>\n\n<p>Update (2018-08-02): <a href=\"https:\/\/twitter.com\/heathborders\/status\/1024868536433471490\">Heath Borders<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/heathborders\/status\/1024868536433471490\">\n<p>If you use <code>-Wnullable-to-nonnull-conversion<\/code> and nullability annotations on local variables and properties, you get proper warnings when you pass a <code>_Nullable<\/code> to a <code>_Nonnull<\/code> parameter or assign a <code>_Nullable<\/code> rvalue to a <code>_Nonnull<\/code> lvalue.<\/p>\n<p>And if you ever need to cast a <code>_Nullable<\/code> to a <code>_Nonnull<\/code>, <a href=\"https:\/\/stackoverflow.com\/questions\/42447553\/how-do-i-safely-cast-a-nullable-to-a-nonnull-in-objective-c\/46123981#46123981\">this<\/a> is the safe way to do so.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Daniel Lazarenko (via Gianluca Bertani): The features listed here are chosen to have a relatively small scope in order to be implemented without major changes and in a backwards compatible way in the spirit of Objective-C. Thus they are not meant to turn Objective-C into a modern experimental language like Swift, but should make programming [&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":"2018-08-02T16:48:06Z","apple_news_api_id":"dbc35d4c-6186-452f-b3ef-60c2041662ca","apple_news_api_modified_at":"2018-08-02T16:48:09Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/A28NdTGGGRS-z72DCBBZiyg","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":[],"tags":[46,571,54,71],"class_list":["post-22326","post","type-post","status-publish","format-standard","hentry","tag-languagedesign","tag-memory-management","tag-objective-c","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22326","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=22326"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22326\/revisions"}],"predecessor-version":[{"id":22342,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/22326\/revisions\/22342"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=22326"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=22326"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=22326"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}