문제

Hi I am a newbie to Objective C and I don't have a mac I'm using online compiler available at compileonline.com. Today I wrote the following program

 1 #import<Foundation/Foundation.h>
 2 int main(){
 3   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];
 4   NSString * firstName = @"Eric";
 5   NSString * lastName  = @"Simmons";
 6   NSString * fullName = [[firstName stringByAppendingString: @" "]stringByAppendingString: lastName];
 7   NSLog(fullName);//prints Eric Simmons
 8   NSLog (@"The Entered full name is :",fullName);// Just prints The entered full name is : 
 9   NSString * correctFullName = [fullName stringByReplacingOccurrencesOfString: firstName withString: @"Derrick"];
 10  NSLog(correctFullName);//prints Derrick Simmons
 11   NSLog(@"The correct full name : ",correctFullName);// Just prints The correct full name is : 
 12  [pool drain];
 13   return 0;
 14 }

However this program works well. But output is not what I expected . Output looks like this

Executing the program....
2014-01-02 05:42:27.958 demo[18115] Eric Simmons
2014-01-02 05:42:27.959 demo[18115] The Entered full name is :
2014-01-02 05:42:27.959 demo[18115] Derrick Simmons
2014-01-02 05:42:27.959 demo[18115] The correct full name : 

The names are not getting printed when the line 8 and 11 are executed. Where I have gone wrong. Is it a error in program or is it bug in compiler. Please help...

도움이 되었습니까?

해결책

In line 8 & 11 You forgot the format specifier

NSLog (@"The Entered full name is : %@",fullName);
NSLog(@"The correct full name : %@",correctFullName);

다른 팁

You need format specifier to print the string.

Use %@ to print strings like below:

 NSLog (@"The Entered full name is : %@",fullName);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top