我是 Objective-C 和 iPhone 的新手
我必须使用值对 NSDictionary 进行排序,并且根据我必须排列该值的键,我已经对数组进行了排序。
//getting values
NSArray* keys1 = [sortDictionary allValues];
NSArray* sortedArray = [keys1 sortedArrayUsingComparator:^(id a, id b) {
return [a compare:b options:NSNumericSearch];
}];
我现在的问题是我通过 allKeysForObject: 获取值的键,它返回一个 NSArray。
for( NSString* aStr in sortedArray ) {
NSLog( @"%@ has value %@", [sortDictionary allKeysForObject:aStr], aStr );
}
O/P :- (
Sourish
) has value 60
我希望将“Sourish”作为字符串存储到 NSArray,然后将该 NSArray 存储到 NSDictionary 或将 Sourish 和 60 存储到另一个 NSDictionary
请让我解决这个问题。
Best Answer-推荐答案 strong>
这会通过查看数组为您获取字符串。这就是你所追求的吗?
NSDictionary* sortDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"60", @"Sourish", @"45" , @"eckish", @"25", @"Swordfish", @"15", @"Mashmish", nil];
NSArray* keys1 = [sortDictionary allValues];
NSArray* sortedArray = [keys1 sortedArrayUsingComparator:^(id a, id b)
{
return [a compare:b options:NSNumericSearch];
}];
for( NSString* aStr in sortedArray )
{
NSString* keyForObject = @"";
if ([[sortDictionary allKeysForObject: aStr] count] > 0)
{
keyForObject = [[sortDictionary allKeysForObject: aStr] objectAtIndex: 0];
}
NSLog( @"%@ has value %@", keyForObject, aStr );
}
此日志:
Mashmish has value 15
Swordfish has value 25
Peckish has value 45
Sourish has value 60
关于iphone - 如何将返回的 NSArray 添加到 NSArray,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/10070487/
|