iPhone ...如果你有一个 NSMutableDictionaries 的 NSArray,该数组是否保存对 dict 的引用或“副本”?
好吧,来自 Perl 背景的我习惯了数组保存哈希值(iPhone 术语中的字典)的引用。
我想知道的是:如果我有一个 NSMutableDictionaries 的 NSArray。假设我将以下 NSMutableDictionary 添加到 NSArray:
[theArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: timestamp, @"timestamp" ,name, @"name" ,lat, @"lat" ,lon, @"lon" , nil]];
然后我像这样循环遍历数组:
for (NSMutableDictionary theDict in theArray)
{
}
每个 theDict
是否代表一个指针 NSMutableDictionary 的数据,以便我可以执行此操作:
for (NSMutableDictionary *theDict in theArray)
{
if ([theDict objectForKey:@"timestamp"] != nil && [[theDict objectForKey:@"timestamp"] isEqualToString:@"timestamp"])
{
[theDict setObject:@"new name" forKey@"name"];
}
}
然后期望 theArray< /code> 在 theArray 的整个生命周期中保存更改的值?
或者,我是否需要这样做:
int ct = 0;
for (NSMutableDictionary *theDict in theArray)
{
if ([theDict objectForKey:@"name"] != nil && [[theDict objectForKey:@"name"] isEqualToString:@"name you want to change"])
{
NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] initWithDictionary:theDict copyItems:YES];
[tmpDict setObject:@"new name" forKey@"name"];
[theDict replaceObjectAtIndex:ct withObject:tmpDict];
[tmpDict release];
break;
}
ct += 1;
}
不可能是所有代码,可以吗? -- 只是为了替换 NSArray 中保存的 NSMutableDictionary 的键的一个值?
抱歉,如果这个问题对某些人来说是显而易见的......但我仍然试图将我的注意力集中在 Objective-c 中指向字典(或对象)的指针上 - with 大致相当于对 perl 中的哈希值的引用 - 与副本Objective-c 中对象的(或深度复制)。 NSArray 持有指向 NSMutableDictionaries 的指针(希望如此),这使得问题变得更加混乱。这就是 Perl 中的实现方式——据我所知——在名称之外(指针与引用).. 在 Objective-c 中也是一样的。
Okay, coming from a perl background I am used to Arrays holding refs to Hashes (Dictionary in iPhone parlance).
What I would like to know is this: If I have a NSArray of NSMutableDictionaries. Say I added the following NSMutableDictionary to an NSArray:
[theArray addObject:[[NSMutableDictionary alloc] initWithObjectsAndKeys: timestamp, @"timestamp" ,name, @"name" ,lat, @"lat" ,lon, @"lon" , nil]];
Then later I cycled through the array like so:
for (NSMutableDictionary theDict in theArray)
{
}
does each theDict
represent a pointer NSMutableDictionary's data such that I can do this:
for (NSMutableDictionary *theDict in theArray)
{
if ([theDict objectForKey:@"timestamp"] != nil && [[theDict objectForKey:@"timestamp"] isEqualToString:@"timestamp"])
{
[theDict setObject:@"new name" forKey@"name"];
}
}
and then expect theArray
to hold the changed value throughout the life of theArray?
or, do I need to do this:
int ct = 0;
for (NSMutableDictionary *theDict in theArray)
{
if ([theDict objectForKey:@"name"] != nil && [[theDict objectForKey:@"name"] isEqualToString:@"name you want to change"])
{
NSMutableDictionary *tmpDict = [[NSMutableDictionary alloc] initWithDictionary:theDict copyItems:YES];
[tmpDict setObject:@"new name" forKey@"name"];
[theDict replaceObjectAtIndex:ct withObject:tmpDict];
[tmpDict release];
break;
}
ct += 1;
}
It cannot be all that code, can it? -- just to replace one value for a key of a NSMutableDictionary held in an NSArray?
Sorry if this question is obvious to some...but still trying to wrap my mind around pointer to a dictionary (or object) in Objective-c -- with is roughly equivalent to a ref to a hash in perl -- vs a copy (or deepcopy) of an object in Objective-c. And the problem is made more confusing by the NSArray holding the pointers (hopefully) to the NSMutableDictionaries. This is how it is done in perl -- and from what I am understanding -- outside of the name (pointer vs reference) .. it is the same in Objective-c.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你应该用第一种方法。每个
theDict
都是指向原始内容的指针,而不是副本。编辑:编辑问题后,答案的其余部分不再相关,但留下来为评论提供上下文,因为您永远不会有太多指向对象所有权文档的链接!
您的
autorelease 与 release
旁边暗示可能对其他事情有误解,但现在可能不是详细讨论这一点的时候。我建议您再看一下 对象所有权docs 以确保您了解autorelease
的真正含义。(顺便说一句,我认为你最初的 tmpArray 应该是 theArray ,除非我完全误解了你的问题。循环变量的类型应该是 NSMutableDictionary*< /code> 而不是
NSMutableDictionary
。)You should do it the first way. Each
theDict
is a pointer to the original, not a copy.Edit: remainder of answer no longer relevant following edit of question, but left around to give context to comments and because you can never have too many links to the object ownership docs!
Your
autorelease vs release
aside suggests a possible misunderstanding about something else, but this perhaps isn't the time to go into that in detail. I suggest you have another look at the object ownership docs to be sure you understand whatautorelease
really means.(Btw, your initial
tmpArray
should betheArray
I think, unless I've totally misunderstood your question. And the type of the loop variables should beNSMutableDictionary*
rather thanNSMutableDictionary
.)