I'm picking up ObjC and Cocoa, which is also my first serious foray into programming in general.
I'm having trouble with the differences between initWith methods, which are called on instances, and factory methods, which are ca开发者_开发问答lled on classes.
Firstly, why are they called "factory" methods, and is there a proper term for what I've dubbed "InitWith" methods?
Secondly, what's the functional difference? Is it just the memory management implications (that factory methods return an autoreleased object)?
For example, what's the real difference between [NSString stringWithString: (NSString*)aString]
and [[NSString alloc] initWithString: (NSString*)aString]
?
The difference between the methods is described in Cocoa's object ownership policy. You own the object returned from -initWithString: and so must release it, but you don't own the object returned from +stringWithString and so do not need to release it (furthermore, if you do want to gain ownership of it, you must retain it).
Factory methods are called that because they return an already-created object for you, usually with parameters you provide that are used to configure the object, for the convenience of the programmer.
On your example of
[[NSString alloc] initwithString: (NSString
*)aString];
When you do an alloc you are placing a space for the String in the memory hence the alloc. you then tell it to initialize with the string equal to aString. the (NSString *) is an identifier for the object type so you are telling it aString is declared to be an NSString.
I usually do something like
NSString * aString = @"String value";
declares what aString is equal to.
If you alloc something into memory in order to manage it you will need to release it at the correct time such as the
-(void) dealloc {}
section of your code
Everything about NSString can be explained in this documentation from Apple NSString
The main difference is that
[NSString stringWithString:
(NSString*)aString]
returns an autoreleased string whose memory management you need not worry about, whereas
[[NSString alloc] initWithString:
(NSString*)aString]
returns a string which you are responsible for releasing. Basically the former is a shortcut for the latter, plus autorelease.
精彩评论