Objective-C noob here.
Why would this:
NSString *myString = [NSString alloc];
[myString initWithFormat:@"%f", storedNumber];
results in the following exception -length only defined for abstract class. Define -[NSPlaceholderString length]!
When this works just fine:
NSString *myString = [[NSString alloc] initWithFormat:@"%f", storedNumber];
I would think that the latter is merely a contraction of the former (but I’m obviously wrong, at least according to the compiler).
Because
-initWithFormat:is returning an object that’s different from the one returned by+alloc, i.e., an object that’s different from the one pointed bymyString. That’s the reason why you should always couple+allocwith-init….This situation is common in class clusters such as
NSString.+allocreturns a generic string object, then-initWithFormat:decides upon a concrete subclass ofNSString, deallocates the current object created by+alloc, creates a new object from a concrete subclass ofNSString, and then returns this new object.