NSString with instance variable

So, I want to put the instance variable in NSString as follows:

NSString * theAnswer = (@ "Answer:% @ \ n", self.answer);

I'm not sure if I'm right or not. I thought NSString would work like NSLog, but it doesn't seem to be that way.

theAnswer only returns an instance variable with no response "Response:

Can someone tell me why and how to solve this problem?

Thanks.

+7
objective-c nsstring
source share
2 answers
NSString *theAnswer = [NSString stringWithFormat:@"The answer is %@", self.answer]; 
+16
source share

I would also like to point out in addition to dj2's answer that NSLog is a method, not an object. Objects are not initialized in the form ("param1", param2) For the NSString case, you do what dj2 did:

 NSString *theAnswer = [[NSString alloc] initWithFormat:@"The answer is %@", self.answer]; 

Where should you declare AAnswer as an NSString pointer, because all Objective-C objects are pointers, and then again tell which class it will be allocated to (in this case NSString), then you will tell how you are going to initialize it in this case too you use initWithFormat: to initialize it.

0
source share

All Articles