开发者

Is it safe to call initializers multiple times on the same 'alloc'ed object?

开发者 https://www.devze.com 2023-01-24 07:21 出处:网络
I can construct a UIImageView object with myImageView = [[UIImageView alloc] initWithImage:image]; Following application activity affecting the display, if I want to change the image on the UIImageVi

I can construct a UIImageView object with myImageView = [[UIImageView alloc] initWithImage:image];

Following application activity affecting the display, if I want to change the image on the UIImageView. I can do so by reassigning it with myImageView.image = someNewImage. However this doesn't seem to update the frame dimensions. I can modify those manually, but I have observed in practice that calling [myImageView initWithImage:someNewImage] does that for me, and has the advantage of being terser.

However I not sure if it is officially a breach of protocol in Objective C to make multiple calls to init methods on an object constructed by a single alloc. I wouldn't use it unless it was safe (guaranteed not to crash or cause leaks). Is there evidence that it is unsafe?

My research so far...

This article gives general detail about 'alloc' and 'init' on objects

http://developer.apple.com/library/mac/documentation/cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html

This is related SO question

Why are alloc and init called separately in Objective-C?

This blog article warns suppliers of objects that their init methods may be called multiple times as an effect of the initialization proc开发者_运维技巧ess.

http://www.friday.com/bbum/2009/09/06/iniailize-can-be-executed-multiple-times-load-not-so-much/


No.

-init is written assuming that it is only called once. For example, -initWithImage quoted in Apple's documentation you quoted is implemented as

- (id)initWithImage:(NSImage *)anImage {
  ...
  if (self) {
      image = [anImage retain];
  }
  return self;
}

This assumes that the ivar image doesn't point to a retained object. If called twice, it leaks image.

Call -init... only once per alloc, and call -init... immediately after alloc by combining them as usual:

SomeClass* foo=[[SomeClass alloc] init...: ... ];

You should never separate them, because [anAllocedObject init...] might return something different from anAllocedObject.


No, init (or one of it's variants) should only be called once on any object, as Yuji explained. As far as your UIImageView issue goes, I believe you can just call [imageView sizeToFit] after assigning your new image, and it will automatically resize it for you.

0

精彩评论

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