Reputation: 13
How would I use a combination of text and variables in a NSString? I know that in an NSLog, it looks like this:
int number = 5; NSLog(@"My favorite number is %i", number);
How would I go about doing something like that in an NSString or even a char variable?
Upvotes: 1
Views: 1061
Reputation: 11646
use all the power of old beloved printf formatting.
for objects "%@" will call the description method, so be smart to write this method for every custom class:
-(NSString*)description;
{
NSString* result = [NSString stringWithFormat(@"%@" .......
}
for example:
-(NSString *)description;
{
return [NSString stringWithFormat:@"is: %@ %@ %@ ; at: %f %f",
name, address, img_name,
coord.latitude, coord.longitude];
}
Upvotes: 0
Reputation: 104698
NSString * str = [NSString stringWithFormat:@"My favorite number is %i", number];
if you just want to read about format specifiers, see: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
Upvotes: 1
Reputation: 10303
That is fairly simple:
NSString *string = [NSString stringWithFormat:@"My favorite number is %i", number];
its basically the same as nslog.
Upvotes: 3