I need a good analogy to make sense of Class methods vs. instance methods

时光毁灭记忆、已成空白 提交于 2019-12-10 09:55:52

问题


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


回答1:


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.




回答2:


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.




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!