NSString has a class method called stringWithString that takes care of alloc, init and handling the release of the newly allocated string object.
As a rule of thumb: class methods that return a "ready-to-go" object also take care of memory management, so no need to call release on an object created through a class method.
NSString has a class method called stringWithString that takes care of alloc, init and handling the release of the newly allocated string object.
As a rule of thumb: class methods that return a "ready-to-go" object also take care of memory management, so no need to call release on an object created through a class method.
Hope this helps.
Thank for the quick reply.
I understand what you are saying about "ready-to-go" object but not sure how I would use NSLog in my code. Can you demonstrate?
I understand what you are saying about "ready-to-go" object but not sure how I would use NSLog in my code. Can you demonstrate?
Cheers.
Sure. It's important to keep in mind, that NSLog simply takes 1 NSString object, so you either need to concatenate multiple strings together using the method "stringByAppendingString" or you can create one yourself using [NSString stringWithFormat] which allows to use placeholders (such as %d, %f, %@, and so on)
Code:
NSString *s = [NSString stringWithString:@"this is the content of the string"];
NSLog([[NSString stringWithString:@"the string contains: "] stringByAppendingString:s]);
NSLog([NSString stringWithFormat:@"the string contains: %@", s]);
Sure. It's important to keep in mind, that NSLog simply takes 1 NSString object, so you either need to concatenate multiple strings together using the method "stringByAppendingString" or you can create one yourself using [NSString stringWithFormat] which allows to use placeholders (such as %d, %f, %@, and so on)
Code:
NSString *s = [NSString stringWithString:@"this is the content of the string"];
NSLog([[NSString stringWithString:@"the string contains: "] stringByAppendingString:s]);
NSLog([NSString stringWithFormat:@"the string contains: %@", s]);