{"id":7625,"date":"2013-07-24T13:12:26","date_gmt":"2013-07-24T18:12:26","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=7625"},"modified":"2013-07-24T13:12:27","modified_gmt":"2013-07-24T18:12:27","slug":"api-smell-nsnumber","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2013\/07\/24\/api-smell-nsnumber\/","title":{"rendered":"API Smell: NSNumber"},"content":{"rendered":"<p><a href=\"http:\/\/confusatory.org\/post\/56153849516\/api-smell-nsnumber\">Christopher Bowns<\/a>:<\/p>\n<blockquote cite=\"http:\/\/confusatory.org\/post\/56153849516\/api-smell-nsnumber\"><p><code>NSNumber<\/code>&rsquo;s weakness lies in the ambiguous type of its value. If you receive <code>NSNumber *aNumber<\/code> as a method parameter and want to know its scalar value, what type should it be evaluated as? Is it correct to treat it as a <code>YES<\/code>\/<code>NO<\/code>, and send it <code>[aNumber boolValue]<\/code>? Or perhaps it&rsquo;s a floating-point value and should be sent <code>[aNumber floatValue]<\/code>? Without introspecting the Objective-C type encoding of an <code>NSNumber<\/code>, a glance at its value doesn&rsquo;t hint at the original value. Consider an <code>NSNumber<\/code> whose <code>integerValue<\/code> is <code>1<\/code>. Is it a signed or unsigned integer? Is it a boolean? Or is it a float that happens to be <code>1.0<\/code>?<\/p><\/blockquote>\n<p>He recommends:<\/p>\n<blockquote cite=\"http:\/\/confusatory.org\/post\/56153849516\/api-smell-nsnumber\"><p>An objective evaluation of one&rsquo;s use of <code>NSNumber<\/code> makes for clearer and more future-proof code. <code>NSNumber<\/code> should be used only as a temporary representation of a numeric value. If an instance of <code>NSNumber<\/code> has numeric meaning, it should be converted to a scalar value; otherwise, it should be converted to an <code>NSString<\/code>.<\/p><\/blockquote>\n<p>Also, if you have an API that uses <code>NSNumber<\/code>, you should document its type. This is not good:<\/p>\n<pre>NSString * const NSURLFileSizeKey; \/\/ Total file size in bytes (Read-only, value type NSNumber)<\/pre>\n<p>I would have assumed that a file size is an <code>unsigned long long<\/code>, but this <a href=\"http:\/\/developer.apple.com\/library\/ios\/documentation\/FileManagement\/Conceptual\/FileSystemProgrammingGUide\/TechniquesforReadingandWritingCustomFiles\/TechniquesforReadingandWritingCustomFiles.html\">Apple sample code<\/a> treats it as a 32-bit signed integer.<\/p>","protected":false},"excerpt":{"rendered":"<p>Christopher Bowns: NSNumber&rsquo;s weakness lies in the ambiguous type of its value. If you receive NSNumber *aNumber as a method parameter and want to know its scalar value, what type should it be evaluated as? Is it correct to treat it as a YES\/NO, and send it [aNumber boolValue]? Or perhaps it&rsquo;s a floating-point value [&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":[69,507,54,71],"class_list":["post-7625","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-json","tag-objective-c","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/7625","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=7625"}],"version-history":[{"count":0,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/7625\/revisions"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=7625"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=7625"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=7625"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}