For example,
-
NSString *string = [NSString stringWithString:@"a string"]; -
NSString *string = [[NSString alloc] initWithString:@"a string"];
and while we’re talking about strings, is there any difference by setting up a string with:
NSString *string = @"a string";
?
As a final note, this isn’t a specific question about NSString. I’m asking on a wider scope of all NSObjects.
There is no difference in ARC, but prior to it there was a difference:
alloc/initreturns an item with ref count of at least one that you’d need toreleasewhen you don’t need it, while the class method returns an autoreleased item that you’d need toretainif you would like to keep it. The ARC compiler knows all this, and takes care of retaining/releasing for you based on your ownership specifications.