在Objective-C中多次调用 [英] Calling -init multiple times in Objective-C
问题描述
当您在一个对象上调用多次时,会发生什么,是否有一些隐藏的副作用?
你能假设没有分配额外的内存吗?
What happens when you call -init multiple times on one object, are there some hidden side effects? Can you assume that no additional memory is allocated? Would anything go against such an idea?
推荐答案
调用 -init
多次未定义,不受支持,会导致错误,崩溃和其他意外行为。
Calling -init
multiple times is undefined, unsupported, and will lead to bugs, crashes, and other unexpected behavior.
code> NSString , NSArray
和 NSDictionary
当调用 + alloc
方法时,实际上不会分配任何 直到各种 -init *
方法之一被调用,对象有足够的上下文,以找出最有效的方式做任何你要求。
Many classes -- NSString
, NSArray
, and NSDictionary
, for example -- don't actually allocate anything when the +alloc
method is called. It isn't until one of the various -init*
methods are called that the object has enough context to figure out the most efficient means of doing whatever you ask.
这篇关于在Objective-C中多次调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!