在 ios 中创建一个新的 NsmutableArray
Create a new NsmutableArray in ios
你好,我是 ios 和 objective c 的新手。我正在做一个演示。因为我有一个用于将值添加到 NSMutableArray
的 for 循环。现在这个数组将被添加到 NSMutableDictionary
。现在在这个 for 循环中,我有一个 if 条件来检查字符串。我在 "if" 部分向数组添加对象,并希望在条件失败时创建一个新数组。我试过如下,但在我的字典中只保存了一个数组。谁能帮助我哪里出错了?
NSMutableArray *listingCommonArr,sectionTitles,eventDate;
int j=0;
NSString *dt;
NSMutableArray *sectionArray = [[NSMutableArray alloc]init];
sectionTitles = [orderedSet array];
NSLog(@"===listing common array---%d",[listingCommonArr count]);
sectionData = [[NSMutableDictionary alloc]init];
for(int i =0;i<[listingCommonArr count];i++)
{
dt = [[listingCommonArr objectAtIndex:i] objectForKey:@"start_date"];
NSLog(@"====my date at place===%@ & my date is====%@",[sectionTitles objectAtIndex:j],dt);
for (int i =0; i < [listingCommonArr count]; i++) {
dt = [[listingCommonArr objectAtIndex:i] objectForKey:@"start_date"];
if ([(NSString *)[sectionTitles objectAtIndex:j] isEqualToString:dt]) {
[sectionArray addObject:listingCommonArr[i]];
//sectionData = @{dt : sectionArray}; // <-- PAY ATTENTION ON THIS LINE, PLEASE
//[sectionData setValue:sectionArray forKey:dt];
sectionData = [@{dt : sectionArray} mutableCopy];
} else {
[sectionData setObject:@"New value" forKey:@"string"];
sectionArray = [[NSMutableArray alloc]init];
j++;
}
}
NSLog(@"====my section DAta is==%@",sectionData);
}
errorTrace
-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7fc6547832a0
2015-12-18 19:39:41.387 iPhoneExploreCayman[41719:13822557] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7fc6547832a0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a551c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a1e8bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010a5590ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010a4af13c ___forwarding___ + 988
4 CoreFoundation 0x000000010a4aecd8 _CF_forwarding_prep_0 + 120
5 iPhoneExploreCayman 0x000000010604a48c -[iPhoneECListingView GetEventListingData:] + 1884
6 iPhoneExploreCayman 0x000000010605644b -[iPhoneECListingView EventListStart] + 1323
7 iPhoneExploreCayman 0x0000000106055a98 -[iPhoneECListingView EventClicked:] + 2312
8 UIKit 0x0000000108832d62 -[UIApplication sendAction:to:from:forEvent:] + 75
9 UIKit 0x000000010894450a -[UIControl _sendActionsForEvents:withEvent:] + 467
10 UIKit 0x00000001089438d9 -[UIControl touchesEnded:withEvent:] + 522
11 UIKit 0x000000010887f958 -[UIWindow _sendTouchesForEvent:] + 735
12 UIKit 0x0000000108880282 -[UIWindow sendEvent:] + 682
13 UIKit 0x0000000108846541 -[UIApplication sendEvent:] + 246
你有 sectionData = @{dt : sectionArray};在 if 部分之外。
所以在每次迭代中你的字典都会被重新创建。
因为我认为您需要 sectionData 的可变字典,并将该字符串移动到 "else" section
下
1 在循环前某处初始化可变字典:
sectionData = [NSMutableDictionary new];
2 将当前数组添加到字典必须在 "else" 块下,据我了解,您应该创建新的 sectionArray:
else
{
[sectionData setObject: sectionArray forKey:dt];
sectionArray = [[NSMutableArray alloc]init];
}
每次调用 sectionData = @{dt : sectionArray};
或设置任何 sectionData
时,都会从字典中删除其他值,只保留最后一对。(每次调用 setObject: forKey:
时都在初始化字典)
如果你想创建可变字典并向其中添加对象,你应该这样做:
NSMutableDictionary *d2 = [NSMutableDictionary new];//sectionData in your case
[d2 addEntriesFromDictionary:@{@"b":@"c"}];//new key value pair added to d2
[d2 addEntriesFromDictionary:@{@"c":@"d"}];//new key value pair added to d2
[d2 addEntriesFromDictionary:@{@"d":@"e"}];//new key value pair added to d2
我对你在这里真正想做什么感到非常困惑,但是......在你发表评论后,我已经完全更新了我的答案,删除了之前关于实际应该发生的事情的假设。
在我看来,您希望(编辑)将一系列 单词 按第一个字母分组。你已经发布了一份动物清单作为例子,所以我使用了这些。
您定义了预期输出:
NSDictionary *_expectedOutput = @{@"A" :@[@"Affrican cat", @"Assian cat", @"Alsesian fox"], @"B" : @[@"Bear", @"Black Swan", @"Buffalo"], @"C" : @[@"Camel", @"Cockatoo"], @"D" : @[@"Dog", @"Donkey"], @"E" : @[@"Emu"], @"R" : @[@"Rhinoceros"], @"S" : @[@"Seagull"], @"T" : @[@"Tasmania Devil"], @"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};
如您所见,我的 input 数组只是样本中动物随机名称的未排序列表 output:
NSArray *_input = @[@"Whale Shark", @"Tasmania Devil", @"Affrican cat", @"Bear", @"Seagull", @"Black Swan", @"Whale", @"Wombat", @"Camel", @"Rhinoceros", @"Cockatoo", @"Buffalo", @"Assian cat", @"Alsesian fox", @"Emu"];
我正在使用 input 数组,我将按第一个字母对它们进行分组并构建 output:
NSMutableDictionary *_output = [NSMutableDictionary dictionary];
[[_input sortedArrayUsingSelector:@selector(compare:)] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *_firstLetter = (obj.length > 0 ? [obj substringToIndex:1] : nil);
if (_firstLetter) {
NSMutableArray *_list = [_output valueForKey:_firstLetter];
if (_list == nil) {
_list = [NSMutableArray arrayWithObject:obj];
[_output setValue:_list forKey:_firstLetter];
} else {
[_list addObject:obj];
}
}
}];
差不多就是这样,如果您记录 _output
,您可以在控制台上看到与您的 _expectedOutput
集合相同的内容。
你好,我是 ios 和 objective c 的新手。我正在做一个演示。因为我有一个用于将值添加到 NSMutableArray
的 for 循环。现在这个数组将被添加到 NSMutableDictionary
。现在在这个 for 循环中,我有一个 if 条件来检查字符串。我在 "if" 部分向数组添加对象,并希望在条件失败时创建一个新数组。我试过如下,但在我的字典中只保存了一个数组。谁能帮助我哪里出错了?
NSMutableArray *listingCommonArr,sectionTitles,eventDate;
int j=0;
NSString *dt;
NSMutableArray *sectionArray = [[NSMutableArray alloc]init];
sectionTitles = [orderedSet array];
NSLog(@"===listing common array---%d",[listingCommonArr count]);
sectionData = [[NSMutableDictionary alloc]init];
for(int i =0;i<[listingCommonArr count];i++)
{
dt = [[listingCommonArr objectAtIndex:i] objectForKey:@"start_date"];
NSLog(@"====my date at place===%@ & my date is====%@",[sectionTitles objectAtIndex:j],dt);
for (int i =0; i < [listingCommonArr count]; i++) {
dt = [[listingCommonArr objectAtIndex:i] objectForKey:@"start_date"];
if ([(NSString *)[sectionTitles objectAtIndex:j] isEqualToString:dt]) {
[sectionArray addObject:listingCommonArr[i]];
//sectionData = @{dt : sectionArray}; // <-- PAY ATTENTION ON THIS LINE, PLEASE
//[sectionData setValue:sectionArray forKey:dt];
sectionData = [@{dt : sectionArray} mutableCopy];
} else {
[sectionData setObject:@"New value" forKey:@"string"];
sectionArray = [[NSMutableArray alloc]init];
j++;
}
}
NSLog(@"====my section DAta is==%@",sectionData);
}
errorTrace
-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7fc6547832a0
2015-12-18 19:39:41.387 iPhoneExploreCayman[41719:13822557] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSDictionaryI setObject:forKey:]: unrecognized selector sent to instance 0x7fc6547832a0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010a551c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010a1e8bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010a5590ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010a4af13c ___forwarding___ + 988
4 CoreFoundation 0x000000010a4aecd8 _CF_forwarding_prep_0 + 120
5 iPhoneExploreCayman 0x000000010604a48c -[iPhoneECListingView GetEventListingData:] + 1884
6 iPhoneExploreCayman 0x000000010605644b -[iPhoneECListingView EventListStart] + 1323
7 iPhoneExploreCayman 0x0000000106055a98 -[iPhoneECListingView EventClicked:] + 2312
8 UIKit 0x0000000108832d62 -[UIApplication sendAction:to:from:forEvent:] + 75
9 UIKit 0x000000010894450a -[UIControl _sendActionsForEvents:withEvent:] + 467
10 UIKit 0x00000001089438d9 -[UIControl touchesEnded:withEvent:] + 522
11 UIKit 0x000000010887f958 -[UIWindow _sendTouchesForEvent:] + 735
12 UIKit 0x0000000108880282 -[UIWindow sendEvent:] + 682
13 UIKit 0x0000000108846541 -[UIApplication sendEvent:] + 246
你有 sectionData = @{dt : sectionArray};在 if 部分之外。 所以在每次迭代中你的字典都会被重新创建。
因为我认为您需要 sectionData 的可变字典,并将该字符串移动到 "else" section
下1 在循环前某处初始化可变字典:
sectionData = [NSMutableDictionary new];
2 将当前数组添加到字典必须在 "else" 块下,据我了解,您应该创建新的 sectionArray:
else
{
[sectionData setObject: sectionArray forKey:dt];
sectionArray = [[NSMutableArray alloc]init];
}
每次调用 sectionData = @{dt : sectionArray};
或设置任何 sectionData
时,都会从字典中删除其他值,只保留最后一对。(每次调用 setObject: forKey:
时都在初始化字典)
如果你想创建可变字典并向其中添加对象,你应该这样做:
NSMutableDictionary *d2 = [NSMutableDictionary new];//sectionData in your case
[d2 addEntriesFromDictionary:@{@"b":@"c"}];//new key value pair added to d2
[d2 addEntriesFromDictionary:@{@"c":@"d"}];//new key value pair added to d2
[d2 addEntriesFromDictionary:@{@"d":@"e"}];//new key value pair added to d2
我对你在这里真正想做什么感到非常困惑,但是......在你发表评论后,我已经完全更新了我的答案,删除了之前关于实际应该发生的事情的假设。
在我看来,您希望(编辑)将一系列 单词 按第一个字母分组。你已经发布了一份动物清单作为例子,所以我使用了这些。
您定义了预期输出:
NSDictionary *_expectedOutput = @{@"A" :@[@"Affrican cat", @"Assian cat", @"Alsesian fox"], @"B" : @[@"Bear", @"Black Swan", @"Buffalo"], @"C" : @[@"Camel", @"Cockatoo"], @"D" : @[@"Dog", @"Donkey"], @"E" : @[@"Emu"], @"R" : @[@"Rhinoceros"], @"S" : @[@"Seagull"], @"T" : @[@"Tasmania Devil"], @"W" : @[@"Whale", @"Whale Shark", @"Wombat"]};
如您所见,我的 input 数组只是样本中动物随机名称的未排序列表 output:
NSArray *_input = @[@"Whale Shark", @"Tasmania Devil", @"Affrican cat", @"Bear", @"Seagull", @"Black Swan", @"Whale", @"Wombat", @"Camel", @"Rhinoceros", @"Cockatoo", @"Buffalo", @"Assian cat", @"Alsesian fox", @"Emu"];
我正在使用 input 数组,我将按第一个字母对它们进行分组并构建 output:
NSMutableDictionary *_output = [NSMutableDictionary dictionary];
[[_input sortedArrayUsingSelector:@selector(compare:)] enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *_firstLetter = (obj.length > 0 ? [obj substringToIndex:1] : nil);
if (_firstLetter) {
NSMutableArray *_list = [_output valueForKey:_firstLetter];
if (_list == nil) {
_list = [NSMutableArray arrayWithObject:obj];
[_output setValue:_list forKey:_firstLetter];
} else {
[_list addObject:obj];
}
}
}];
差不多就是这样,如果您记录 _output
,您可以在控制台上看到与您的 _expectedOutput
集合相同的内容。