【发布时间】:2012-08-10 02:27:58
【问题描述】:
我正在使用包含的方法返回指向 NSMutableDictionary 的指针,该指针包含在 NSArray 中。然而,NSMutableArray (theOne) 被创建为一个不可变的 NSDictionary。这是一个问题,因为我想在使用此方法检索字典后对其进行修改。
- (NSMutableDictionary*)getMatFromBoutKey:(NSString*) boutKey
{
/*
* Returns the mat object with the provided boutKey.
* Returns nil if no mat has that boutKey.
*/
NSUInteger idx = [[event objectForKey:@"mats"] indexOfObjectPassingTest:
^ BOOL (NSMutableDictionary* obj, NSUInteger idx, BOOL *stop)
{
return [[obj objectForKey:@"boutKey"] isEqualToString:boutKey];
}];
if (idx == NSNotFound)
return nil;
else {
NSMutableDictionary* theOne = [[event objectForKey:@"mats"] objectAtIndex: idx];
return theOne;
}
}
这是调试器在第一次引用 theOne 后在断点处停止的图像。
为什么 theOne 不是可变的?如何返回指向 NSMutableDictionary 的指针,以便在获得返回给我的值后对其进行修改?
谢谢!
【问题讨论】:
-
[event objectForKey:@"mats"] 是可变字典数组吗?
-
什么是事件?这是从哪里来的?
-
不要在方法前面加上
get。那应该只是matFromBoutKey:。 -
是的,[event objectForKey:@"mats"] 是一个可变字典数组。我在另一个班级创建事件。 Event 是该类和保存上述代码的类的属性。谢谢@bbum,我会做出改变的。
标签: objective-c nsmutablearray objective-c-blocks