I am wondering if [[alloc] init]
is just a convention or if separating the two calls has wider use. For instance, I wonder if people ever call [init]
(or friends) on an existing object to "re-initialize" it.
相关问题
- how to define constructor for Python's new Nam
- Keeping track of variable instances
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Object.create() bug?
相关文章
- 现在使用swift开发ios应用好还是swift?
- 接口B继承接口A,但是又不添加新的方法。这样有什么意义吗?
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- didBeginContact:(SKPhysicsContact *)contact not in
- Custom Marker performance iOS, crash with result “
By convention, the only use of the init family is when they call each other. For example:
You might pass around a zombie object where init had never been called, but I cannot think of a good reason to do so. If you want to create an object without knowing the type, you pass around a Class.
Edit:
alloc
guarantees that all members are initialized to zero, and manyinit
methods rely on that. Callinginit
more than once breaks that contract.It can do, if memory is highly constrained (as in an iPhone application) you can 'alloc' one (or a few) instances and then reuse them. You need to be careful about leaks though, as objects retained/created in init are often released/freed at the 'free' message.
Interesting question. Personally I’d think twice before I started messing with this. It seems to be just begging for a new source of bugs while not bringing anything new that you could not obtain in a safer way. Maybe I just don’t understand what’s going on in alloc and init enough to play with those parts, but I’d found that “dumb” code generally makes me happier (KISS).
The convention is to never ever re-initialize objects. You can do so on an extremely limited basis in your own classes that subclass NSObject (or defining a new root), but you should never do so with a subclass of any other class from the system frameworks.
If you want to "re-use" instances of a class, then provide methods for re-use beyond the designated initializer. I.e. you could do:
Or: