Objective-C nsmutablearray setValue forKey 问题

发布于 2024-12-02 00:14:01 字数 1730 浏览 0 评论 0原文

我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

灼疼热情 2024-12-09 00:14:01

NSMutableArray 不存储键/值对,它只存储对象。要将对象添加到数组中,请使用 -addObject: 方法。

使用数组来存储对象的有序列表。当您需要查找给定某个键的对象时,请使用字典。如果您想要字典键的有序列表,请使用

NSArray* orderedKeys = 
    [[myDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];

编辑:还有几个选项。

  1. 只需存储一个索引数组,然后在主字符串数组中查找值。

    for (NSUInteger i = 0; i < [数组计数]; ++i)
    {
        NSString* s = [数组 objectAtIndex:i];
        NSRange ran = [s rangeOfString:@"abc"];
        if (ran.location != NSNotFound)
        {
            [mutarray addObject:[NSNumber numberWithInt:i]];
        } 
    }
    
    for (NSNumber* mutarray 中的索引)
    {
        NSLog(@"找到 %@:%@", index, [array objectAtIndex:[index intValue]]);
    }         
    
  2. 存储索引和字符串的二维数组:

    for (NSUInteger i = 0; i < [数组计数]; ++i)
    
    {
        NSString* s = [数组 objectAtIndex:i];
        NSRange ran = [s rangeOfString:@"abc"];
        if (ran.location != NSNotFound)
        {
            [mutarray addObject:
                [NSArray arrayWithObjects:[NSNumber numberWithInt:i],
                                          s,
                                          零]]; 
        } 
    }
    

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

NSArray* orderedKeys = 
    [[myDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];

Edit: A couple of more options.

  1. Just store an array of indexes, then look up the value in your main array of strings.

    for (NSUInteger i = 0; i < [array count]; ++i)
    {
        NSString* s = [array objectAtIndex:i];
        NSRange ran = [s rangeOfString:@"abc"];
        if (ran.location != NSNotFound)
        {
            [mutarray addObject:[NSNumber numberWithInt:i]];
        } 
    }
    
    for (NSNumber* index in mutarray)
    {
        NSLog(@"Found %@:%@", index, [array objectAtIndex:[index intValue]]);
    }         
    
  2. Store a two-dimensional array of indexes and strings:

    for (NSUInteger i = 0; i < [array count]; ++i)
    
    {
        NSString* s = [array objectAtIndex:i];
        NSRange ran = [s rangeOfString:@"abc"];
        if (ran.location != NSNotFound)
        {
            [mutarray addObject:
                [NSArray arrayWithObjects:[NSNumber numberWithInt:i],
                                          s,
                                          nil]]; 
        } 
    }
    
寻找一个思念的角度 2024-12-09 00:14:01

NSArray 是对象的有序集合。它不是对象的键值映射。在 NSArray 上调用 setValue:forKey: 与向其每个项目发送 setValue:forKey: 相同。

NSArray is an ordered collection of objects. It is not a key-value mapping of objects. Calling setValue:forKey: on an NSArray is the same as sending setValue:forKey: to each of its items.

请爱~陌生人 2024-12-09 00:14:01

这个 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:

沉睡月亮 2024-12-09 00:14:01

看起来您想存储一个有序的配对列表。所以 NSArray 的 NSArray

for (NSString *s in array)
{
    NSRange ran = [s rangeOfString:@"abc"];
    if (ran.location != NSNotFound)
    {
        int inde = [array indexOfObject:s];
        NSArray* pair = [NSArray arrayWithObjects:[NSNumber numberWithInt:inde],s,nil];
        [mutarray addObject: pair];
    } 
}

It looks like you want to store an ordered list of pairs. So NSArray of NSArrays

for (NSString *s in array)
{
    NSRange ran = [s rangeOfString:@"abc"];
    if (ran.location != NSNotFound)
    {
        int inde = [array indexOfObject:s];
        NSArray* pair = [NSArray arrayWithObjects:[NSNumber numberWithInt:inde],s,nil];
        [mutarray addObject: pair];
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文