IPhone: formatted string issues (target C)

I need help. Why this does not work:

NSProcessInfo *process = [NSProcessInfo processInfo]; NSString *processName = [process processName]; int processId = [process processIdentifier]; NSString *processString = [NSString stringWithFormat:@"Process Name: @% Process ID: %f", processName, processId]; NSLog(processString); 

But it does:

 NSLog(@"Process Name: %@ Process ID: %d", [[NSProcessInfo processInfo] processName], [[NSProcessInfo processInfo] processIdentifier]); 
+4
source share
3 answers
  • %@ : print the string form of the object (including NSString ).
  • %f : print floating point number ( float )
  • %d : Print an integer ( int )
  • %x : print the hex form of a number

Your original NSString:stringWithFormat: had two problems:

  • @% must be %@ to output NSString.
  • You use %f instead of %d to output int.
+14
source

Your format string is bad: processId is an int not a float.

Use -Wformat to get rid of such errors.

+2
source

Your format contains an error, you replaced @ and % with [NSString stringWithFormat:] . It will work for a log, but not for creating a string, since the format is %@ not @% .

0
source

All Articles