开发者

Objective C - preferred way to create and initialize an object

开发者 https://www.devze.com 2022-12-22 23:47 出处:网络
Is one of these two ways to create and initialize an object preferable? MyClass oClass = [[MyClass alloc] init];

Is one of these two ways to create and initialize an object preferable?

MyClass oClass = [[MyClass alloc] init];
oClass.length = 5;
oClas开发者_C百科s.text = @"Hello";

or using a class method that contains about the same code but looks like this:

MyClass oClass = [MyClass myClassWithLength:(int) 5 andText:(NSString *) @"Hello"];

I hate it when I see things done more than one way and I have no idea if one is better than the other, or why!


Don't be a hater. :-)

By the way, I'm assuming you meant:

(Edit: removed unnecessary casts)

MyClass oClass = [[MyClass alloc] initWithLength:5 andText:@"Hello"];

The reason for multiple init... methods is to make it more convenient for developers to create properly initialized instances. So, for example, if you find that developers often need to create instances of MyClass with a length and text, you make their life easier by providing an API that allows them to do that in one step. And if you find that developers also frequently need to create instances of MyClass with just a text string, you might also provide an -initWithText: method.

And if the instances created this way are frequently used as temporary objects (i.e., not stored in instance variables or static variables), you might also add a class convenience method like +myClassWithText: that returns an autoreleased instance of MyClass initialized with the provided text string.

As to which one is better: it's always better to fully initialize an object when possible, so if the object needs both values to be properly initialized, use the method that allows you to provide both arguments. And if you don't need to store a reference to the instance you're creating, use the class convenience method so your code doesn't have to deal with memory management.


If the object is unusable without the length and text, then the second option might be better. If those fields are optional, then the first one is better.

However, I don't think there is absolute truth to this question.


If you have a class with many properties it's very unlikely to initialize them all in one single line of code. Both ways work fine for me.


If an initWithSomething: method is available and you want to provide initial values for those properties, I would always prefer it just because it's simpler. It also will always work even with immutable versions of a class.

But neither init method is inherently "better." Classes usually have one or two designated initializers and all the others just call those with default values — it doesn't necessarily leave the instance's properties untouched. The documentation for a class should indicate what its initializers do and which is the designated initializer for the class. For example, [[NSDate alloc] init] uses NSDate's designated initializer, initWithTimeIntervalSinceReferenceDate:, to create a date object representing the current date and time.

Incidentally, this also means that when you're subclassing a class, you only need to override its designated initializer. Since the others just call that, they get your new behavior for free.

0

精彩评论

暂无评论...
验证码 换一张
取 消