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 > Can't set BOOL

Can't set BOOL
Thread Tools
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 6, 2002, 02:20 PM
 
I can't seem to set a BOOL. I have the variable declared in the header file and I have the following accessor methods:

-(void)setNewReference BOOL)yesOrNo {

newReference=yesOrNo;
}
-(BOOL)newReference {
return newReference;
}


I'm then calling the following method:

-(void) loadReference NSString *)referenceID {

NSString *referenceString;
NSString *referencesResultString;
NSURL *referenceURL;
[self setNewReference:NO];
NSLog(@"newRef=%@",newReference);
//some other stuff
}

The log output says
2002-04-06 12:17:01.679 CocoaMed[25000] newRef=(null)

It's as if the BOOL is never being generated. Can anyone offer any help?

thanks,
kman
     
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Apr 6, 2002, 04:42 PM
 
Based on the quick glance I took at your code it looks like your problem is with how you are dereferencing the BOOL in the NSLog line. Since saying "%@" in cocoa is a macro to dereference an API object, you are getting (null) because you can't dereference a BOOL. This is because a BOOL is a primitive type. I am not sure how to format a string such that BOOL is converted to "YES" or "NO" but, since I don't think there is any built-in mechanism to do that, you could just dereference it as an integer (using "%d") and then read it as 1 or 0.

That would change your NSLog line to be:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> NSLog(<font color = orange>@"newRef=%d"</font>,newReference);
</font>[/code]

Then if newReference is true, you will get "newRef=1" or if false you will get "newRef=0".

Try that,
Jeff.
Spectral Class
"Shedding Light on Innovation"
     
kman42  (op)
Professional Poster
Join Date: Sep 2000
Location: San Francisco
Status: Offline
Reply With Quote
Apr 6, 2002, 05:17 PM
 
Actually, that %d gives me a type error.

Here's something else interesting. If I change the code to [self setNewReference:YES] the app crashes during the setNewReference call. It runs through if I try to set it to NO, but not to YES.

kman
     
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Apr 6, 2002, 07:49 PM
 
Try casting the BOOL to int at the point of the call. That should solve your type error.

The reason why that is crashing on true is exactly because of the explanation I gave surrounding dereferencing. If you try to dereference a NULL cocoa object it detects that and just outputs (null) but if you try to dereference something that is not a valid pointer (but not null) in that way it will crash. This is because a BOOL NO = 0 and BOOL YES = 1. Trying to dereference 0 is NULL so that failsafe kicks in but 1 could be a valid memory address (although not really) so it tries to dereference it which crashes your program because it is trying to read beyond your program's address space.

Try casting the BOOL to int like this and it _should_ work:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier> NSLog(<font color = orange>@"newRef=%d"</font>,(int)newReference);
</font>[/code]

I think that should produce the effect that you want (more or less). Perhaps if you tried wrapping the BOOL in an NSNumber it would behave the way you currently have it coded but that is usually overkill for the storage of one bit of data.

Jeff.
Spectral Class
"Shedding Light on Innovation"
     
   
Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:52 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2