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

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 18, 2009, 06:15 PM   #1
redchannel
macrumors regular
 
Join Date: Nov 2008
Questions about the newline character \n

I have an example of code that I need help understanding:

#import <Foundation/Foundation.h>

{

int main (int argc, const char *argv [])

NSAutoreleasePool *pool =[ [NSAutoreleasePool alloc] init];

NSLog [@"Testing...\n...1\n...2\n...3");

[pool drain];

retuen 0;

}


is the \n (newline command) another way to say NSLog [@"Insert text here"] without having to type in the NSLog stuff (sorry i dont know the exact terminology of this thing) ?

I know its a new line on the output. When the program launches will it simply say

Testing...
...1
...2
...3 ?


on the scree ? Is this just a program to display text ?
redchannel is offline   0 Reply With Quote
Old Jun 18, 2009, 06:36 PM   #2
wlh99
macrumors 6502
 
Join Date: Feb 2008
Newline is like the enter key. Output after the newline will start on the next line. Without it ..

NSLog (@"One ");
NSLog (@"Two");

produces:
One Two

The program actually does nothing. NSLog writes to the log (usually the console view that is available in XCode) when testing a program in the Debug configuration. In a release configuration NSLog is ignored.

To actually write to the iPhone screen is much more complicated.

If you are interested in learning iPhone or Mac programming, read the stickies and invest in a recomended book. You should be much further than this snippet by the first couple pages.
wlh99 is offline   0 Reply With Quote
Old Jun 18, 2009, 06:50 PM   #3
dejo
Moderator
 
dejo's Avatar
 
Join Date: Sep 2004
Location: The Centennial State
Quote:
Originally Posted by wlh99 View Post
NSLog (@"One ");
NSLog (@"Two");

produces:
One Two
No, it doesn't. It produces something like this:
2009-06-18 17:49:48.729 AppName[7265:20b] One
2009-06-18 17:49:48.730 AppName[7265:20b] Two

whereas
Code:
NSLog(@"Testing...\n...1\n...2\n...3");
produces something like this:
2009-06-18 17:52:49.905 AppName[7304:20b] Testing...
...1
...2
...3
dejo is offline   0 Reply With Quote
Old Jun 18, 2009, 07:03 PM   #4
Saladinos
macrumors 68000
 
Saladinos's Avatar
 
Join Date: Feb 2008
Newline characters are one of several special glyphs. These glyphs exist because simply typing in the effect they represent may cause problems in certain situations.

For example, to include a quotation mark, type \". Typing " would confuse the compiler, which would think you meant to close the string. Similarly, new lines can cause errors and the newline character could simply be ignored by the compiler.

And just in case, you were wondering, if you want to type a normal \, type \\. So \\n would show up as \n rather than a newline.
Saladinos is offline   0 Reply With Quote
Old Jun 18, 2009, 08:04 PM   #5
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Quote:
Originally Posted by wlh99 View Post
In a release configuration NSLog is ignored.
NSLog() works on Release and Debug. If you want it to not work on Release you need to do something like:
Code:
#if DEBUG
#define Log NSLog
#else
#define Log 
#endif
And use Log() instead of NSLog().
kainjow is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad 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
I am doing a contest about the iPhone and i need a hard question about the iPhone Grigio2009 iPhone 18 Jun 25, 2009 08:06 AM
So here's the wrap-up for the months of questions about the iPhone The Californian iPhone 5 Jun 8, 2009 06:44 PM
Question about the correct MVC layout of app and the best way to access the Modal luckylefty01 iPhone/iPad Programming 4 Jul 7, 2008 07:07 AM
A question about iPod Nanos (I Have one but a question about the memory) willyhunt iPod 4 Sep 10, 2007 03:45 PM
I have a quick question about the Radions on the G5... TheOne Macintosh Computers 1 Jun 24, 2003 09:34 PM


All times are GMT -5. The time now is 07:39 PM.

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

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