|
|
Using NSString as boolean
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
Is there a way to explicitly typecast NSStrings @"YES" or @"NO" as booleans? I've run into some inconsistencies with testing such objects (they have to be NSStrings not BOOLs because they need to be stored in an NSDictionary).
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
NSNumber would be more appropriate for this. It's meant for storing scalars in object containers like NSDictionary.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
Oops, thanks. Didn't think of NSNumber.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status:
Offline
|
|
Don't use NSNumber for this... Apple has implemented a system in the compiler that caches some forms of NSNumber so that there is only one instance. This is a great win for speed, but can have some unexpected consequences if you are using it like you are proposing to use it.
The best solution is to create a quick class that inherits from NSObject and has a single member variable (a BOOL) and a getter and a setter (the latter only if you need to change it). You can even make factory methods to produce one with FALSE and one with TRUE.
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
Really? I've used NSNumber for this kind of thing before. Do you have any links that explain exactly what Apple's done? I'm pretty curious.
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
I implemented it with NSNumber and haven't experienced any problems so far. I'd also like to know more about the problems you mentioned.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status:
Offline
|
|
NSOrdinal numbers up to 12 (I think...) now all get represented by a single instance of NSNumber (since it is immutable anyways). I have had several people who were admittedly doing odd things run across problems because they were dealing with each reference to it as a reference to a private object (things like comparing the pointers, or working with the memory allocation closely).
But ultimately, you would be better off with your own class because it would be lighter weight than the NSNumber meta-class. It is a shame that there is not an Obj-C BOOL wrapper...
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status:
Offline
|
|
You could always use (id)kCFBooleanTrue and (id)kCFBooleanFalse.
When comparing any NSObjects, you shouldn't do pointer-based equality testing unless you're absolutely SURE that they'll be the same pointers, which is rare. So instead of using ==, you should be using isEqual: or, if you know the classes, one of the optimized versions like isEqualToString:.
Pointer-based stuff rarely works because a lot of times, Cocoa will copy an object transparently. Like, if you use an NSString as a key in an NSDictionary, Cocoa actually uses a copy of the NSString. So if you try to use pointer-equality testing on the key later, the result'll always be false. This stuff is documented, but it's easy to miss.
|
Geekspiff - generating spiffdiddlee software since before you began paying attention.
|
|
|
|
|
|
|
|
Senior User
Join Date: Nov 2001
Location: State of Denial
Status:
Offline
|
|
Originally Posted by larkost
It is a shame that there is not an Obj-C BOOL wrapper...
I'm pretty sure this is what +[NSNumber numberWithBool:], etc., are for.
(Also, what smeger said.)
|
[Wevah setPostCount:[Wevah postCount] + 1];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|