Im getting fairly confused as the book im reading is delving into the NSNumber class and talks about all the different methods you can call on it. I have a couple questions:
1.) Do you not have to call a typical alloc or init on foundation classes?
2.)in what cases would you use, say, numberWithChar: as opposed to initWithChar (i think this is the part that is messing me up the most, not really sure im groking this concept on the level i need to be, if you folks could break it down for me I think it would really help me get over this humper-roo.
Thanks,
Nick
Class/Instance Analogies
Classes are like blueprints for a type house. Instances are like actual houses. So you can only have one blueprint for a single type of house, but you can have multiple actual houses of the same type. Also, you can have multiple blueprints, and each blueprint describes a different type of house.
Another analogy you can use is that classes are like cookie cutters, and instances are like cookies made from a cookie cutter.
How It Applies To Objective-C
There is one "class object" for every class in your code. To refer to the class object, you just use the class name. alloc
is a class method that allocates a new instance like so:
MyWidget* w = [MyWidget alloc];
However, alloc
doesn't initialize the class, so none of the member variables will be set up. init
is an instance method that will initialize a newly allocated instance. So to allocate and initialize a new instance, you do this:
MyWidget* w = [[MyWidget alloc] init];
Which is equivalent to this:
MyWidget* w = [MyWidget alloc]; //alloc is being called on the class
w = [w init]; //init is being called on the instance
Another common type of class method is a factory method like numberWithChar:
. This is basically what numberWithChar:
does:
+(NSNumber*) numberWithChar:(char)c;
{
return [[[NSNumber alloc] initWithChar:c] autorelease];
}
The only real difference is that numberWithChar:
returns an autoreleased object.
All objects must be allocated and initialized. That includes foundation classes.
1) alloc
and init
need to be called virtually always. numberWithChar
is a convenience method, which means it calls alloc
, init
, and autorelease
for you and returns the autoreleased instance.
2) Since numberWithChar
returns an autoreleased object, that means unless you retain it (or pass it to something like an NSArray
which will retain
it), it'll be destroyed shortly. initWithChar
returns a retain
-ed object, which means you have to release it when you're done with it.
I found when I was starting out that it was helpful to use init
-alloc
as a rule, instead of the convenience methods, because it made me pay close attention to my memory management.
The difference between class and instance methods is addressed from a number of angles in the answers to this question What is the difference between class and instance methods?
EDIT:
To be honest, the analogy I use when I call a class method on, say NSString
, is praying to the god of NSStrings to bestow upon me a magnificent new NSString
. Notice that class methods are almost 100% used for creation. e.g. alloc
is a class method, stringWithFormat
is a class method, and so on.
Yes, it's ridiculous, I know.
Ok, first thing: In Objective-C, you don't call a method, you send a message to an object. The runtime looks up the methods and calls them.
It might help you to think of a class as a factory. In fact, years ago, we used to refer to class methods as factory methods.
来源:https://stackoverflow.com/questions/2032538/i-need-a-good-analogy-to-make-sense-of-class-methods-vs-instance-methods