Discussion Forums  >  Uncategorized

Replies: 4    Views: 317

Going-Viral
Apple Fan
Profile
Posts: 120
Reg: Jun 13, 2011
Doncaster
1,200
07/17/11 11:33 AM (14 years ago)

NSLog - How to use it?

I know this must be a really dumb ass question to ask on here, but I believe in asking if I don't know, well how on earth are you expected to learn otherwise :-) Anyway, my question is, how to use NSLog to output responses to the debugger. For example, I wish to see how data is being addressed, formatted, etc... In the file BT_screen_menuSearch.m I am trying to see what if any values are passed to the function 'textFieldDidChange' also what values might be held in the variables menuItems, filteredMenuItems, displayMenuItems. Any help would be much appreciated.
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
07/18/11 02:38 AM (14 years ago)
No questions are dumb. NSLog is super duper useful. All it does is print something to the output console. But, like you're discovering, thats very very necessary sometimes. There are some formatting tricks to getting the output the way you need. In the simplest form, it's NSLog(@'print this to the screen'); The single quotes that show in this forum should be double quotes in Xcode. For more sophistated formatting, like numbers, dates, and other non-string values, do a quick Google Search. You'll end up with log statements that look like... Example...to print a number to the console: NSLog(@'my number is%d', 34); The 34 will be printed where the %d is. Weird I know but anything that's not a generic string message will need a format modifier or it won't print. Again...google's your friend.
 
Going-Viral
Apple Fan
Profile
Posts: 120
Reg: Jun 13, 2011
Doncaster
1,200
like
07/18/11 02:48 AM (14 years ago)
Many thanks, so is %d a variable within your string output to which you are passing 34 or is it a formatting element to determine how 34 will be displayed in the debugger?
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
07/18/11 03:40 AM (14 years ago)
The %d is just a format modifier (fancy speak for a placeholder. NSLog(@'my number %d', 34) will output 'my number 34' NSLog(@'my first name is %@ and my last name is %@, 'david', 'book') outputs 'my first name is david and my last name is book' The type of modifier is determined by the type of data you want to output. Number, string, decimal, etc. Saw your email, I'll try to get a look tom. It's 3:30 AM here. Done.
 
Going-Viral
Apple Fan
Profile
Posts: 120
Reg: Jun 13, 2011
Doncaster
1,200
like
07/18/11 04:29 AM (14 years ago)
Thanks, I get it now :) 12 noon here, I think you may need some sleep LOL :-)
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.