简体   繁体   中英

Trying to understand NSLog for printing to the console in Xcode

I'm trying to understand NSLog and how to print to the console in Xcode. I understand that NSLog uses what are called "tokens" to setup the type of variable being referenced to print (I think that's right?). What I need to know is the difference in which tokens to use and what they mean?

For example, after declaring an NSArray like below, I'd want to print the drink names to the console. I could do that like:

NSArray *drinks = @[@"juice", @"water", @"coffee"];

for (NSString *drinkName in drinks) {
    NSLog(@"%@", drinkName);
}

So...am I using the @"%@" token because it's an NSString ?

I would use @"%i" for an integer, and a @"%f" for a float? What about doubles? If anyone could shed some easy-to-understand beginner's knowledge on NSLog, that would be great! =)

As I understand it, NSLog isn't an Objective C function but a C function built into the foundation of Cocoa. Therefore it conforms to basic C functions with variadic arguments. You can use %@ for all objects including NSString. This will in turn call the objects description method and print the appropriate string. Most objects have a rather useful representation already there (eg NSArray objects return the descriptions of all their contents)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM