我的代码:
NSArray *array = //objects here
NSMutableArray *mutarray = [[NSMutableArray alloc] init];
for (NSString *s in array)
{
NSRange ran = [s rangeOfString:@"abc"];
if (ran.location != NSNotFound)
{
int inde = [array indexOfObject:s];
[mutarray setValue:[NSNumber numberWithInt:inde] forKey:s];
}
}
NSLog (@"mutarray: %@", mutarray);
我试过 NSMutableDictionary,但是内容是无序的,所以我更喜欢 NSMutableArray 有序地存储 Values 和 Keys。但是 NSLog 的输出什么也没显示,到底是什么?NSMutableDictionary 的输出显示了四个发现。
编辑:
我什至在 NSLog 前面尝试了这段代码,但没有显示:
[mutarray setValue:@"aha" forKey:@"jaha"];
编辑:伙计们,谢谢你的回答!现在我明白了。但问题是我将数字存储为值,将字符串存储为键。我有一个无序的 NSMutableDictionary。我还没有弄清楚我应该如何订购它。我有我使用的代码,但没有正确编写:
NSInteger intSort(id num1, id num2, void *context)
{
int v1 = [num1 intValue];
int v2 = [num2 intValue];
if (v1 < v2)
return NSOrderedAscending;
else if (v1 > v2)
return NSOrderedDescending;
else
return NSOrderedSame;
}
在其他方法中:
NSArray *ordered = [[mutdic allValues] sortedArrayUsingFunction:intSort context:NULL];
NSLog (@" ordered: %@", ordered);
输出仅显示:
2011-08-29 22:36:03.998 scanner2[32359:207] ordered: (
1,
5,
9,
16
)
输出应按数字顺序显示:
2011-08-29 22:36:03.992 scanner2[32359:207] mutdic: 5 jam (geologi) en
2011-08-29 22:36:03.993 scanner2[32359:207] mutdic: 9 jax. (geologi)jammen
2011-08-29 22:36:03.994 scanner2[32359:207] mutdic: 1 beta (geologi).
2011-08-29 22:36:03.995 scanner2[32359:207] mutdic: 16 .(geologi),be;ta;