Objective-C nsmutablearray setValue forKey 问题
我的代码:
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;
My codes:
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);
I have tried NSMutableDictionary, but the content is unordered, so I prefer NSMutableArray to store Values and Keys orderly. But the output of NSLog showed nothing, what in the hell?
The output of NSMutableDictionary showed four discoveries.
EDIT:
I even tried this code in front of the NSLog, but nothing shows:
[mutarray setValue:@"aha" forKey:@"jaha"];
EDIT:
Guys, thanks for the answer! Now I understand. But the problem is that I store numbers as values and strings as keys. I have an NSMutableDictionary which is unordered. I still havent figured out how I should order it. I have the codes which I use, but it is not properly written:
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;
}
In other method:
NSArray *ordered = [[mutdic allValues] sortedArrayUsingFunction:intSort context:NULL];
NSLog (@" ordered: %@", ordered);
The output shows only:
2011-08-29 22:36:03.998 scanner2[32359:207] ordered: (
1,
5,
9,
16
)
The output should show this in ordered numerically:
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NSMutableArray
不存储键/值对,它只存储对象。要将对象添加到数组中,请使用-addObject:
方法。使用数组来存储对象的有序列表。当您需要查找给定某个键的对象时,请使用字典。如果您想要字典键的有序列表,请使用
编辑:还有几个选项。
只需存储一个索引数组,然后在主字符串
数组
中查找值。存储索引和字符串的二维数组:
NSMutableArray
does not store key/value pairs, it only stores objects. To add an object to the array, use the-addObject:
method.Use an array to store an ordered list of objects. Use a dictionary when you need to look up an object given some key. If you want an ordered list of a dictionary's keys, use
Edit: A couple of more options.
Just store an array of indexes, then look up the value in your main
array
of strings.Store a two-dimensional array of indexes and strings:
NSArray
是对象的有序集合。它不是对象的键值映射。在NSArray
上调用setValue:forKey:
与向其每个项目发送setValue:forKey:
相同。NSArray
is an ordered collection of objects. It is not a key-value mapping of objects. CallingsetValue:forKey:
on anNSArray
is the same as sendingsetValue:forKey:
to each of its items.这个 setValue:ForKey: 方法不是来自 NSMutableArray,它是 NSKeyValueCodingProtocol
在你的情况下,由于你想要从字典中排序结果,你可以看看以下方法:
–keysSortedByValueUsingSelector:
–keysSortedByValueUsingComparator:
–keysSortedByValueWithOptions:usingComparator:
如果需要对值进行排序,只需使用返回数组的 allKeys 即可。您可以使用以下任一方法对数组进行排序:
–sortedArrayHint
–sortedArrayUsingFunction:上下文:
–sortedArrayUsingFunction:上下文:提示:
– 使用描述符排序数组:
–sortedArrayUsingSelector:
–sortedArrayUsingComparator:
–sortedArrayWithOptions:使用比较器:
This setValue:ForKey: method is not from the NSMutableArray, it is NSKeyValueCodingProtocol
In your case, since you want ordered results from a dictionary, you could take a look at the following methods:
– keysSortedByValueUsingSelector:
– keysSortedByValueUsingComparator:
– keysSortedByValueWithOptions:usingComparator:
If you need values sorted, just use allKeys which returns an array. You can them sort the array using any of the following methods:
– sortedArrayHint
– sortedArrayUsingFunction:context:
– sortedArrayUsingFunction:context:hint:
– sortedArrayUsingDescriptors:
– sortedArrayUsingSelector:
– sortedArrayUsingComparator:
– sortedArrayWithOptions:usingComparator:
看起来您想存储一个有序的配对列表。所以 NSArray 的 NSArray
It looks like you want to store an ordered list of pairs. So NSArray of NSArrays