NSDictionaries 的 NSMutableArray
NSMutableArray of NSDictionaries
我在 NSMutableArray
中有多个 NSDictionaries
。下面是一个速记示例。 comparisonChart
是 NSMutableArray
.
NSDictionary *dict1 = @{
@"Tube": @"10/0",
@"Dress":@"3"
};
[self.comparisonChart setValue:dict1 forKey@"0"];
// key 0 as i wish to use numeric indexes, comparisonChart is mutable array
当我想提取我尝试过的键的值时:
[self.comparisonChart valueForKey:[NSString stringWithFormat:@"%ld", value]]
//where value is numeric value e.g 0
然而这个returns无效。我也试过 objectForKey:value
结果相同。
我该怎么做?
更新:
[self.comparisonChart insertObject:dict23 atIndex:1];
NSLog(@"chart: %@", [self.comparisonChart objectAtIndex:1]);
Output: chart: (null) // why is this?
如果 self.comparisonChart
是一个 NSMutableArray 那么你可以像这样添加 NSDictionary:
[self.comparisonChart addObject: dict1];
或者您可以像这样指定索引:
[self.comparisonChart insertObject: dict1 atIndex:desiredIndex];
要检索 NSDictionary 对象,您必须调用:
[self.comparisonChart objectAtIndex:indexNumber];
你需要使用 objectForKey 作为一个,你也应该使用下标来代替;
NSDictionary *dict1 = @{@"Tube": @"10/0",
@"Dress":@"3"};
NSMutableDictionary *comparisonChart = [NSMutableDictionary new];
comparisonChart[@"0"] = dict1;
NSLog(@"My value: %@", comparisonChart[@"0"]);
这是从 AppDelegate 复制和粘贴的代码,并且可以正常工作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *dict1 = @{@"Tube": @"10/0",
@"Dress":@"3"};
NSMutableDictionary *comparisonChart = [NSMutableDictionary new];
comparisonChart[@"0"] = dict1;
NSLog(@"My value: %@", comparisonChart[@"0"]);
return YES;
}
编辑:
要追加上述问题,请确保插入的索引根据以下文档有效:
- (void)insertObject:(id)anObject
atIndex:(NSUInteger)index
Parameters
anObject
The object to add to the array's content. This value must not be nil.
IMPORTANT
Raises an NSInvalidArgumentException if anObject is nil.
index
The index in the array at which to insert anObject. This value must not be greater than the count of elements in the array.
IMPORTANT
Raises an NSRangeException if index is greater than the number of elements in the array.
Discussion
If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room.
Note that NSArray objects are not like C arrays. That is, even though you specify a size when you create an array, the specified size
is regarded as a “hint”; the actual size of the array is still 0. This
means that you cannot insert an object at an index greater than the
current count of an array. For example, if an array contains two
objects, its size is 2, so you can add objects at indices 0, 1, or 2.
Index 3 is illegal and out of bounds; if you try to add an object at
index 3 (when the size of the array is 2), NSMutableArray raises an
exception.
Import Statement
import Foundation
Availability
Available in iOS 2.0 and later.
我在 NSMutableArray
中有多个 NSDictionaries
。下面是一个速记示例。 comparisonChart
是 NSMutableArray
.
NSDictionary *dict1 = @{
@"Tube": @"10/0",
@"Dress":@"3"
};
[self.comparisonChart setValue:dict1 forKey@"0"];
// key 0 as i wish to use numeric indexes, comparisonChart is mutable array
当我想提取我尝试过的键的值时:
[self.comparisonChart valueForKey:[NSString stringWithFormat:@"%ld", value]]
//where value is numeric value e.g 0
然而这个returns无效。我也试过 objectForKey:value
结果相同。
我该怎么做?
更新:
[self.comparisonChart insertObject:dict23 atIndex:1];
NSLog(@"chart: %@", [self.comparisonChart objectAtIndex:1]);
Output: chart: (null) // why is this?
如果 self.comparisonChart
是一个 NSMutableArray 那么你可以像这样添加 NSDictionary:
[self.comparisonChart addObject: dict1];
或者您可以像这样指定索引:
[self.comparisonChart insertObject: dict1 atIndex:desiredIndex];
要检索 NSDictionary 对象,您必须调用:
[self.comparisonChart objectAtIndex:indexNumber];
你需要使用 objectForKey 作为一个,你也应该使用下标来代替;
NSDictionary *dict1 = @{@"Tube": @"10/0",
@"Dress":@"3"};
NSMutableDictionary *comparisonChart = [NSMutableDictionary new];
comparisonChart[@"0"] = dict1;
NSLog(@"My value: %@", comparisonChart[@"0"]);
这是从 AppDelegate 复制和粘贴的代码,并且可以正常工作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *dict1 = @{@"Tube": @"10/0",
@"Dress":@"3"};
NSMutableDictionary *comparisonChart = [NSMutableDictionary new];
comparisonChart[@"0"] = dict1;
NSLog(@"My value: %@", comparisonChart[@"0"]);
return YES;
}
编辑:
要追加上述问题,请确保插入的索引根据以下文档有效:
- (void)insertObject:(id)anObject
atIndex:(NSUInteger)index
Parameters anObject The object to add to the array's content. This value must not be nil. IMPORTANT Raises an NSInvalidArgumentException if anObject is nil. index The index in the array at which to insert anObject. This value must not be greater than the count of elements in the array. IMPORTANT Raises an NSRangeException if index is greater than the number of elements in the array. Discussion If index is already occupied, the objects at index and beyond are shifted by adding 1 to their indices to make room. Note that NSArray objects are not like C arrays. That is, even though you specify a size when you create an array, the specified size
is regarded as a “hint”; the actual size of the array is still 0. This means that you cannot insert an object at an index greater than the current count of an array. For example, if an array contains two objects, its size is 2, so you can add objects at indices 0, 1, or 2. Index 3 is illegal and out of bounds; if you try to add an object at index 3 (when the size of the array is 2), NSMutableArray raises an exception.
Import Statement import Foundation Availability Available in iOS 2.0 and later.