Re: Converting from scalar to NSNumber*
Re: Converting from scalar to NSNumber*
- Subject: Re: Converting from scalar to NSNumber*
- From: Rick Mann <email@hidden>
- Date: Fri, 27 Feb 2015 16:46:47 -0800
> On Feb 27, 2015, at 16:45 , Greg Parker <email@hidden> wrote:
>
>
>> On Feb 27, 2015, at 4:28 PM, Rick Mann <email@hidden> wrote:
>>
>> I'm updating some older Core Data code in which I made liberal use of transient properties to map NSNumber* types to scalar types like uint32_t. In practice, this doesn't gain much, especially with boxing syntax, and it makes the Core Data classes messier (shadow attributes, etc.).
>>
>> The problem is, if I change an attribute's type to NSNumber*, and fail to modify every reference to that attribute, I often end up comparing pointers, not values:
>>
>> @property (strong) NSNumber* someAttribute;
>>
>> if (obj.someAttribute > 42)
>>
>> which needs to become:
>>
>> if (obj.someAttribute.integerValue > 42)
>>
>> I'd love an explicit warning for misuse of NSNumber*, but a pointer-integer comparison warning would be helpful. However, I don't see that among the available warnings in Xcode.
>
> Here's how to answer "is there a warning for X".
> 1. Write a test file with the line of code that you want to be warned about.
> 2. Compile that file with -Weverything which enables literally every warning supported by the compiler.
> 3. If the compiler warns about the code then the diagnostic message will print the name of the warning flag, if any. Example:
> test.m:13:1: warning: control reaches end of non-void function [-Wreturn-type]
> If it doesn't print a warning name then that warning is on all the time, I think.
>
> When I run a test of your code, I get a warning unconditionally. It's possible that my compiler is newer than yours; in your compiler there might be no warning, or there might be a warning behind some warning flag. Try it yourself.
>
> test.m:12:13: warning: ordered comparison between pointer and integer
> ('NSNumber *' and 'int')
> if (t.x > 42) printf("bigger");
> ~~~ ^ ~~
Great suggestion. Some versions of GCC will also tell you the command-line option that controls the warning it's emitting. I wish clang did that.
--
Rick Mann
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden