在将指针添加到可变数组后,我可以重用它吗?
假设我有一个带有字符串的数组。
NSArray *names = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
我想要的是启动一些自定义类的对象,并将它们添加到可变数组中。我正在使用带有字符串参数的自定义 init 方法。
更具体地说,我想要 [SomeClass alloc] initWithName: aName] 并将生成的对象添加到 NSMutableArray 中。
我正在考虑使用 Objective-C 快速枚举。所以我得到的是:
NSMutableArray *objects = [NSMutableArray arrayWithCapacity: [names count];
for (NSString *name in names) {
[objects addObject: [[[SomeClass alloc] initWithName: name] autorelease]];
}
问题是我无法将 nil
添加到数组中,而且我不喜欢异常处理。但是,我的启动方法可能会返回nil
。所以我决定先检查再添加(预防)。我的新 for-in 循环是:
SomeClass *someObject;
for (NSString *name in names) {
someObject = [[[SomeClass alloc] initWithName: name] autorelease];
if (someObject) {
[objects addObject: someObject];
}
}
现在,我不是立即将新对象传递给数组,而是先设置一个指针 someObject
,然后将指针传递给数组。
这个例子向我提出了一个问题。当我在循环中 someObject = [[[SomeClass alloc] initWithName: name] autorelease]
时,数组中的现有对象(使用同一指针添加)是否也会改变?
换句话说: addObject: (id)someObject
方法是否为我传递的指针创建一个新的内部副本,或者我是否必须创建指针的副本 - 我不知道如何——并亲自传递副本?
多谢! :-)
Let's say I've got an array with strings.
NSArray *names = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
What I want is to initiate objects of some custom class and them add them to a mutable array. I'm using a custom init method that takes a string argument.
To be more specific, I want to [SomeClass alloc] initWithName: aName]
and add the resulting object to a NSMutableArray
.
I'm thinking of using Objective-C fast enumeration. So what I get is:
NSMutableArray *objects = [NSMutableArray arrayWithCapacity: [names count];
for (NSString *name in names) {
[objects addObject: [[[SomeClass alloc] initWithName: name] autorelease]];
}
The problem is that I can't add nil
to the array and I don't like exception handling. However, my initiation method may return nil
. So I decide to check first before adding (prevention). My new for-in-loop is:
SomeClass *someObject;
for (NSString *name in names) {
someObject = [[[SomeClass alloc] initWithName: name] autorelease];
if (someObject) {
[objects addObject: someObject];
}
}
Now, instead of immediately passing the new object to the array, I'm setting up a pointer someObject
first and then passing the pointer to the array instead.
This example raises a question to me. When I someObject = [[[SomeClass alloc] initWithName: name] autorelease]
in the loop, do the existing objects (which are added using the same pointer) in the array change too?
To put it in other words: does the addObject: (id)someObject
method make a new internal copy of the pointer I pass or do I have to create a copy of the pointer — I don't know how — and pass the copy myself?
Thanks a lot! :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
重用
someObject
就可以了;如果你仔细想想,你就会发现,每次执行循环时,你都已经在重复使用name
了。-addObject:
可能会也可能不会复制您传入的对象。(它不会 - 它保留对象而不是复制它,但可以想象某些 NSMutableArray 子类可以改为复制。)重要的是,这段代码实际上不应该关心-addObject:
的作用。另外,不要忽视指针和它所指向的对象之间的区别。指针只是引用,每次将指针传递给方法或函数时都会复制指针。 (与 C 一样,Objective-C 按值传递参数,因此将指针传递到方法中会导致将指针的值放入堆栈中。)但是,对象本身不会被复制。
It's fine to reuse
someObject
; if you think about it, you're already reusingname
each time you go through the loop.-addObject:
may or may not copy the object that you pass in. (It doesn't -- it retains the object rather than copying it, but it's conceivable that some NSMutableArray subclass could copy instead.) The important thing is that this code really shouldn't care about what-addObject:
does.Also, don't lose sight of the distinction between a pointer and the object that it points to. Pointers are just references, and a pointer is copied each time you pass it into a method or function. (Like C, Objective-C passes parameters by value, so passing a pointer into a method results in putting the value of the pointer on the stack.) The object itself isn't copied, however.
简短的回答:不,您不必担心重用
someObject
。答案稍长一些:赋值——
someObject = ...
将一个新的指针值赋给someObject
变量;addObject:
然后获取该值,而不是someObject
本身的地址。Short answer: no, you don't have to worry about reusing
someObject
.Slightly longer answer: the assignment—
someObject = ...
assigns a new pointer value to thesomeObject
variable;addObject:
is then getting that value, not the address ofsomeObject
itself.我认为您在这里对指针的概念感到困惑。当您说
someObject = [[[SomeClass alloc] init...
时,您基本上将someObject
指针指向一个新对象。所以回答你的问题 - 你当前的代码很好。至于数组是否维护添加到其中的对象的副本 - 否,数组保留您添加到其中的对象。但是,这对您上面的代码并不重要。
I think you're getting confused in the concept of pointer here. When you say
someObject = [[[SomeClass alloc] init...
you are basically pointing thesomeObject
pointer to a new object. So to answer your question- your current code is fine.As for whether arrays maintain copies of the objects added to them - NO, the array retains the object you add to it. However, that doesn't matter to your code above.
Three20 提供了答案!
Three20 provides the answer!