Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Using NSString as boolean

Using NSString as boolean
Thread Tools
wataru
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Aug 12, 2005, 07:57 PM
 
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).
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 12, 2005, 08:04 PM
 
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'."
     
wataru  (op)
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Aug 12, 2005, 08:07 PM
 
Oops, thanks. Didn't think of NSNumber.
     
larkost
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Aug 13, 2005, 12:29 PM
 
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.
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Aug 13, 2005, 02:33 PM
 
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'."
     
wataru  (op)
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Aug 13, 2005, 02:36 PM
 
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.
     
larkost
Mac Elite
Join Date: Oct 1999
Location: San Jose, Ca
Status: Offline
Reply With Quote
Aug 13, 2005, 06:37 PM
 
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...
     
smeger
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Aug 14, 2005, 03:22 AM
 
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.
     
Wevah
Senior User
Join Date: Nov 2001
Location: State of Denial
Status: Offline
Reply With Quote
Aug 18, 2005, 01:11 AM
 
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 Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:54 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,