سؤال

I need to show one line via two NSLog? I thought that the code

int i = 0;
NSLog(@"Number ");
...
NSLog(@"%d", i);

show

Number 0

but it shows

Number
0

NSLog automatically inserts a new row. How can I add a line of the first NSLog?

Here is my code:

...
NSLog(@"%@ ", [array objectAtIndex:0]);
if(i==0) NSLog(@"undefined");
else NSLog(@"%d", i);
هل كانت مفيدة؟

المحلول

NSString myString = @"";

if(i==0) myString = @"undefined";
else myString = [NSString stringWithFormat:@"%i", i];

NSLog(@"%@ %@", [array objectAtIndex:0], myString);

Note: %i and %d both stands for integer type.

نصائح أخرى

int i = 0;
NSLog(@"Number %i",i);

NSLog is like printf, where you can build the string right inline.

int a = 1;
int b = 2;
int c = a + b;

NSLog(@"%d + %d = %d", a, b, c);

Output

1 + 2 = 3

Try like this in proper format:-

 NSString *myString = @"";
 if(i==0) 
 {
  myString =[NSString    
  stringWithFormat:@"%@,%@",[array  
  objectAtIndex:0],@"undefined"];
  }
  else 
{ 
 myString =[NSString     
 stringWithFormat:@"%@,%d",[array  
objectAtIndex:0],i];
 }
NSLog(@"%@",myString);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top