Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 29, 2009, 05:58 PM   #1
jardeon
macrumors newbie
 
Join Date: Jul 2007
Displaying Boolean values

Hi everyone,

I've been noodling around a bit in Objective-C and Xcode, and I've run into some trouble in trying to log output using NSLog.



Code:
BOOL yesFlag = YES;
BOOL noFlag = NO;
NSLog(@"This string should end with NO: %@", noFlag);
NSLog(@"This string should end with YES:  %@", yesFlag);
I get the following:
Code:
2009-01-29 18:55:08.707 PrintingBooleans[26312:10b] This string should end with NO: (null)
then a crash, then the following error: Program received signal: "EXC_BAD_ACCESS".

If I change the code so that the string interpolation is using decimals instead, like so:
Code:
BOOL yesFlag = YES;
BOOL noFlag = NO;
NSLog(@"This string should end with NO: %d", noFlag);
NSLog(@"This string should end with YES:  %d", yesFlag);
I get the following.

Code:
2009-01-29 18:57:00.065 PrintingBooleans[26331:10b] This string should end with NO: 0
2009-01-29 18:57:00.093 PrintingBooleans[26331:10b] This string should end with YES:  1
What's the proper Obj-C/XCode way to get this output:

Code:
2009-01-29 18:57:00.065 PrintingBooleans[26331:10b] This string should end with NO: NO
2009-01-29 18:57:00.093 PrintingBooleans[26331:10b] This string should end with YES:  YES
Thanks!
jardeon is offline   0 Reply With Quote
Old Jan 29, 2009, 06:06 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
BOOL is not an object, it is typedef'd to a primitive (can't remember which, not at a mac) with YES and NO defined as macros... 1 and 0 accordingly.

So you can do a few things... i think this is somewhat elegant:
Code:
BOOL yesFlag = YES;
BOOL noFlag = NO;
NSLog(@"This string should end with NO: %@", noFlag?@"YES":@"NO");
NSLog(@"This string should end with YES:  %@", yesFlag?@"YES":@"NO");
You could certainly write a function or macro that returns an NSString with the appropriate value when passed a BOOL as well.

You are passing a primitive to NSLog and telling NSLog it is an Object with %@. You could also just print with %d as you saw, since BOOL is some sort of number, but you will just get 0 or 1, not a word as your output shows.

-Lee
lee1210 is offline   0 Reply With Quote
Old Jan 29, 2009, 06:15 PM   #3
jardeon
Thread Starter
macrumors newbie
 
Join Date: Jul 2007
Thanks Lee!

The ternary operation was exactly what I was looking for, that worked like a charm.

- Jared
jardeon is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting 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

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Boolean Values in C?? cybrscot Mac Programming 17 Mar 24, 2011 06:57 PM
help with boolean value for wireless connection creganbrendan iPhone/iPad Programming 4 Nov 23, 2010 07:04 AM
23 inch apple display resale value? Shadylane315 Mac Peripherals 3 Jun 24, 2010 02:54 AM
UITableViewCell Selection, Return Boolean value trojanvillage iPhone/iPad Programming 0 Oct 18, 2009 10:40 AM
UISlider with animations displaying slider value. jjgraz iPhone/iPad Programming 1 Mar 19, 2009 07:41 AM


All times are GMT -5. The time now is 03:15 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC