tags:

views:

98

answers:

3

Does objective C have a general print command like Python? NSLog seems to log it rather than print out to console. printf only accepts specific types.

+8  A: 

NSLog() does print to the console, and is very similar to C's printf(). Having its origins and basis in C, console printing is done as it is in C, essentially.

phoebus
Was about to say the same thing! +1
Jacob Relkin
Oh, I left out the @ - still new to objective C
Casebash
(Was testing it with a string)
Casebash
NSLog(@"%@", myObject); will achieve the desired goal.
Casebash
A: 

printf is what you're looking for. You can use it like a regular print statement:

printf("This is a neat command!\n");

You're also probably aware that you can use it with substitutions:

printf("The Answer is %d\n", 42);
Dave DeLong
+2  A: 

You can use NSString to format strings containing id types as well as the standard printf types, then just print it using printf:

NSString *fmt = [NSString stringWithFormat:@"My formatted string: %@", anObject];

printf("%s", [fmt cStringUsingEncoding:[NSString defaultCStringEncoding]]);
Barry Wark
It should be `printf("%s",[fmt cStringUsingEncoding:…])`, to cover the case where the string already has a '%' in it.
Chris Johnsen
Yes good point. I'll fix the post.
Barry Wark