{"id":2419,"date":"2014-11-19T16:50:54","date_gmt":"2014-11-19T12:50:54","guid":{"rendered":"https:\/\/blog.alwawee.ru\/?p=2419"},"modified":"2014-11-20T17:45:58","modified_gmt":"2014-11-20T13:45:58","slug":"check-nsdictionary-key-check-value-key-nil","status":"publish","type":"post","link":"https:\/\/blog.alwawee.ru\/?p=2419","title":{"rendered":"How to safely work with NSDictionary that may have empty values or may not have a key"},"content":{"rendered":"<p><a href=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x-300x250.png\" alt=\"Dictionary_2x\" width=\"300\" height=\"250\" class=\"aligncenter size-medium wp-image-2430\" srcset=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x-300x250.png 300w, https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x.png 870w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>NSDictionary sometimes can contain a key with empty values or even may not contain a key, that we expect to have. This is a very common problem, when you work with server APIs and parse network JSON responses to a NSDictionary. In this post I will describe how to safely work with NSDictionary that may have empty values or may not have a key.<\/p>\n<p>The method <strong>objectForKey<\/strong> will return <strong>nil<\/strong>, if there is no a key, a key doesn&#8217;t exist in a dictionary. <\/p>\n<p>You can&#8217;t add <strong>nil<\/strong> to a <strong>NSDictionary<\/strong>. <strong>NSDictionary<\/strong> will throw an <strong>NSInvalidArgumentException<\/strong> if you attempt to insert a nil value. You would have to use <strong>[NSNull null]<\/strong> instead. NSNull is something called nothing, while nil is simply nothing.<\/p>\n<p>Checks that value for key is not empty. <\/p>\n<pre>\r\nif (dic[@\"key\"] != (id)[NSNull null]) {\r\n}\r\n<\/pre>\n<p>But if a dic doesn&#8217;t contain a key, you will enter to the body of a condition <strong>dic[@&#8221;key&#8221;] != (id)[NSNull null]<\/strong>.<\/p>\n<p>For example, before taking <strong>integerValue<\/strong> from obj, you must be sure, that it is not <strong>[NSNull null]<\/strong>:<\/p>\n<pre>\r\nid obj = dic[@\"key\"];\r\nif (obj != (id)[NSNull null]) {\r\n   cityId = [obj integerValue];\r\n}\r\n<\/pre>\n<p>Checks that the key exists, but value for it can still be empty.<\/p>\n<pre>\r\nif (dic[@\"key\"] != nil) {\r\n}\r\n<\/pre>\n<p>So the optimal check while reading NSDictionary is this, where key is &#8220;key&#8221;<\/p>\n<pre>\r\nid obj = dic[@\"key\"];\r\nif ((obj != nil) && (obj != (id)[NSNull null])) {\r\n}\r\n<\/pre>\n<p>You can&#8217;t add <strong>nil<\/strong> to a <strong>NSDictionary<\/strong>. So you have to check, that your object is not nil before adding: <\/p>\n<pre>\r\nif (obj != nil) {\r\n   NSDictionary *dic = @{@\"key\":obj};\r\n}\r\n<\/pre>\n<p>But you can add [NSNull null]: <\/p>\n<pre>\r\nid obj = [NSNull null];\r\nNSDictionary *dic = @{@\"key\":obj};\r\n<\/pre>\n<p>Version of code, where you check if dictionary entry exists and not empty: <\/p>\n<pre>\r\nid obj = dic[@\"key\"];\r\nif ( obj != nil ) {\r\n    if ( obj != (id)[NSNull null] ) {\r\n        \/\/ Use obj to get data\r\n    }\r\n    else {\r\n        NSLog(@\"Warning! ClassName: key section is empty.\");\r\n    }\r\n}\r\nelse {\r\n    NSLog(@\"Warning! ClassName: key section is not found.\");\r\n}\r\n<\/pre>\n<p>I have created a NSDictionary category that has a method to check if a key exists and value is not empty: <\/p>\n<pre>\r\n#import \"NSDictionary+CheckKey.h\"\r\n\r\n@implementation NSDictionary (CheckKey)\r\n\r\n\r\n- (BOOL)checkKey:(NSString *)key className:(NSString *)className\r\n{\r\n    BOOL success = NO;\r\n    id obj = self[key];\r\n    if ( obj != nil ) {\r\n        if ( obj != (id)[NSNull null] ) {\r\n            success = YES;\r\n        }\r\n        else {\r\n            NSLog(@\"Warning! %@: %@ section is empty.\", className, key);\r\n        }\r\n    }\r\n    else {\r\n        NSLog(@\"Warning! %@: %@ section is not found.\", className, key);\r\n    }\r\n    \r\n    return success;\r\n}\r\n\r\n\r\n@end\r\n<\/pre>\n<p>This is an example, how to use it: <\/p>\n<pre>\r\nif ([dic checkKey:@\"key\" className:NSStringFromClass(self.class)]) {\r\n    self.object = dic[@\"key\"];\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>NSDictionary sometimes can contain a key with empty values or even may not contain a key, that we expect to have. This is a very common problem, when you work with server APIs and parse network JSON responses to a NSDictionary. In this post I will describe how to safely work with NSDictionary that may [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[156,155,157],"class_list":["post-2419","post","type-post","status-publish","format-standard","hentry","category-ios","tag-nil","tag-nsdictionary","tag-nsnull"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to safely work with NSDictionary that may have empty values or may not have a key - Denis Kutlubaev<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.alwawee.ru\/?p=2419\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to safely work with NSDictionary that may have empty values or may not have a key - Denis Kutlubaev\" \/>\n<meta property=\"og:description\" content=\"NSDictionary sometimes can contain a key with empty values or even may not contain a key, that we expect to have. This is a very common problem, when you work with server APIs and parse network JSON responses to a NSDictionary. In this post I will describe how to safely work with NSDictionary that may [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.alwawee.ru\/?p=2419\" \/>\n<meta property=\"og:site_name\" content=\"Denis Kutlubaev\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/alwaweemobile\" \/>\n<meta property=\"article:published_time\" content=\"2014-11-19T12:50:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-11-20T13:45:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x-300x250.png\" \/>\n<meta name=\"author\" content=\"Denis Kutlubaev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Denis Kutlubaev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419\"},\"author\":{\"name\":\"Denis Kutlubaev\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"headline\":\"How to safely work with NSDictionary that may have empty values or may not have a key\",\"datePublished\":\"2014-11-19T12:50:54+00:00\",\"dateModified\":\"2014-11-20T13:45:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419\"},\"wordCount\":283,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/11\\\/Dictionary_2x-300x250.png\",\"keywords\":[\"nil\",\"NSDictionary\",\"NSNull\"],\"articleSection\":[\"iOS\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419\",\"name\":\"How to safely work with NSDictionary that may have empty values or may not have a key - Denis Kutlubaev\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/11\\\/Dictionary_2x-300x250.png\",\"datePublished\":\"2014-11-19T12:50:54+00:00\",\"dateModified\":\"2014-11-20T13:45:58+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419#primaryimage\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/11\\\/Dictionary_2x.png\",\"contentUrl\":\"https:\\\/\\\/blog.alwawee.ru\\\/wp-content\\\/uploads\\\/2014\\\/11\\\/Dictionary_2x.png\",\"width\":870,\"height\":726},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/?p=2419#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/blog.alwawee.ru\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to safely work with NSDictionary that may have empty values or may not have a key\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#website\",\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/\",\"name\":\"Denis Kutlubaev\",\"description\":\"iOS Development\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blog.alwawee.ru\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blog.alwawee.ru\\\/#\\\/schema\\\/person\\\/58f1f1f57155b48afab83ba53b3e26a7\",\"name\":\"Denis Kutlubaev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg\",\"caption\":\"Denis Kutlubaev\"},\"description\":\"iOS Developer, creator of Tornado Browser and many other popular apps\",\"sameAs\":[\"http:\\\/\\\/dennis.alwawee.com\"],\"url\":\"https:\\\/\\\/blog.alwawee.ru\\\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to safely work with NSDictionary that may have empty values or may not have a key - Denis Kutlubaev","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.alwawee.ru\/?p=2419","og_locale":"en_US","og_type":"article","og_title":"How to safely work with NSDictionary that may have empty values or may not have a key - Denis Kutlubaev","og_description":"NSDictionary sometimes can contain a key with empty values or even may not contain a key, that we expect to have. This is a very common problem, when you work with server APIs and parse network JSON responses to a NSDictionary. In this post I will describe how to safely work with NSDictionary that may [&hellip;]","og_url":"https:\/\/blog.alwawee.ru\/?p=2419","og_site_name":"Denis Kutlubaev","article_publisher":"https:\/\/www.facebook.com\/alwaweemobile","article_published_time":"2014-11-19T12:50:54+00:00","article_modified_time":"2014-11-20T13:45:58+00:00","og_image":[{"url":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x-300x250.png","type":"","width":"","height":""}],"author":"Denis Kutlubaev","twitter_misc":{"Written by":"Denis Kutlubaev","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blog.alwawee.ru\/?p=2419#article","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/?p=2419"},"author":{"name":"Denis Kutlubaev","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"headline":"How to safely work with NSDictionary that may have empty values or may not have a key","datePublished":"2014-11-19T12:50:54+00:00","dateModified":"2014-11-20T13:45:58+00:00","mainEntityOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2419"},"wordCount":283,"commentCount":0,"image":{"@id":"https:\/\/blog.alwawee.ru\/?p=2419#primaryimage"},"thumbnailUrl":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x-300x250.png","keywords":["nil","NSDictionary","NSNull"],"articleSection":["iOS"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blog.alwawee.ru\/?p=2419#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blog.alwawee.ru\/?p=2419","url":"https:\/\/blog.alwawee.ru\/?p=2419","name":"How to safely work with NSDictionary that may have empty values or may not have a key - Denis Kutlubaev","isPartOf":{"@id":"https:\/\/blog.alwawee.ru\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.alwawee.ru\/?p=2419#primaryimage"},"image":{"@id":"https:\/\/blog.alwawee.ru\/?p=2419#primaryimage"},"thumbnailUrl":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x-300x250.png","datePublished":"2014-11-19T12:50:54+00:00","dateModified":"2014-11-20T13:45:58+00:00","author":{"@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7"},"breadcrumb":{"@id":"https:\/\/blog.alwawee.ru\/?p=2419#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.alwawee.ru\/?p=2419"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.alwawee.ru\/?p=2419#primaryimage","url":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x.png","contentUrl":"https:\/\/blog.alwawee.ru\/wp-content\/uploads\/2014\/11\/Dictionary_2x.png","width":870,"height":726},{"@type":"BreadcrumbList","@id":"https:\/\/blog.alwawee.ru\/?p=2419#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.alwawee.ru\/"},{"@type":"ListItem","position":2,"name":"How to safely work with NSDictionary that may have empty values or may not have a key"}]},{"@type":"WebSite","@id":"https:\/\/blog.alwawee.ru\/#website","url":"https:\/\/blog.alwawee.ru\/","name":"Denis Kutlubaev","description":"iOS Development","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.alwawee.ru\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.alwawee.ru\/#\/schema\/person\/58f1f1f57155b48afab83ba53b3e26a7","name":"Denis Kutlubaev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","url":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/346de75d27c818be0aa7fddeada2e50e30cc15b4da6b3b8110c5d766cd2afa34?s=96&d=mm&r=pg","caption":"Denis Kutlubaev"},"description":"iOS Developer, creator of Tornado Browser and many other popular apps","sameAs":["http:\/\/dennis.alwawee.com"],"url":"https:\/\/blog.alwawee.ru\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2419"}],"version-history":[{"count":22,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2419\/revisions"}],"predecessor-version":[{"id":2460,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=\/wp\/v2\/posts\/2419\/revisions\/2460"}],"wp:attachment":[{"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.alwawee.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}