Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Problems with Formatted String (Objective C)

I need help. How come 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 this does:

NSLog(@"Process Name: %@ Process ID: %d", [[NSProcessInfo processInfo] processName], [[NSProcessInfo processInfo] processIdentifier]);
like image 897
Devoted Avatar asked Dec 06 '22 05:12

Devoted


1 Answers

  • %@: Output the string form of an object (including NSString).
  • %f: Output a floating point number (float)
  • %d: Output an integral number (int)
  • %x: Output hexadecimal form of a number

Your original NSString:stringWithFormat: had two issues:

  1. @% should be %@ to output an NSString.
  2. You use %f instead of %d to output an int.
like image 57
drewh Avatar answered Dec 18 '22 15:12

drewh