有没有办法在 valueForKeyPath 语句中指定特定的对象索引?

发布于 2025-01-05 23:05:21 字数 451 浏览 1 评论 0原文

在调用 NSDictionary valueForKeyPath 时,如果路径的一层是数组,您可以指定数组中的特定元素吗?

例如:

[myDict valueForKeypath:@"customer.contactInfo.phoneNumbers[4].countryCode];

其中 .phoneNumbers 是一个 NSArray。或者我是否必须这样做:

NSArray *numbers=[myDict valueForKeypath:@"customer.contactInfo.phoneNumbers];
NSString *countryCode=[[numbers objectAtIndex:4] objectForKey:@"countryCode"];

如果这可以在一个声明中完成,那就真的很好而且更干净。

In calling NSDictionary valueForKeyPath if one level of the path is an array can you specify a specific element in the array?

For example:

[myDict valueForKeypath:@"customer.contactInfo.phoneNumbers[4].countryCode];

where .phoneNumbers is an NSArray. Or do I have to:

NSArray *numbers=[myDict valueForKeypath:@"customer.contactInfo.phoneNumbers];
NSString *countryCode=[[numbers objectAtIndex:4] objectForKey:@"countryCode"];

It would be really nice and much cleaner if this could be done in one statement.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

天冷不及心凉 2025-01-12 23:05:21

你可以这样做:

NSString *myString = [[[myDict valueForKeypath:@"customer.contactInfo.phoneNumbers"] objectAtIndex:4] objectForKey:@"countryCode"];

You could do this:

NSString *myString = [[[myDict valueForKeypath:@"customer.contactInfo.phoneNumbers"] objectAtIndex:4] objectForKey:@"countryCode"];
披肩女神 2025-01-12 23:05:21

这是我为 NSObject 编写的一个类别,它可以处理数组索引,以便您可以像这样访问嵌套对象:“customer.contactInfo.phoneNumbers[4].countryCode”

@interface NSObject (ValueForKeyPathWithIndexes)
   -(id)valueForKeyPathWithIndexes:(NSString*)fullPath;
@end


#import "NSObject+ValueForKeyPathWithIndexes.h"    
@implementation NSObject (ValueForKeyPathWithIndexes)

-(id)valueForKeyPathWithIndexes:(NSString*)fullPath
{
    //quickly use standard valueForKeyPath if no arrays are found
    if ([fullPath rangeOfString:@"["].location == NSNotFound)
        return [self valueForKeyPath:fullPath];

    NSArray* parts = [fullPath componentsSeparatedByString:@"."];
    id currentObj = self;
    for (NSString* part in parts)
    {
        NSRange range = [part rangeOfString:@"["];
        if (range.location == NSNotFound)           
        {
            currentObj = [currentObj valueForKey:part];
        }
        else
        {
            NSString* arrayKey = [part substringToIndex:range.location];
            int index = [[[part substringToIndex:part.length-1] substringFromIndex:range1.location+1] intValue];
            currentObj = [[currentObj valueForKey:arrayKey] objectAtIndex:index];
        }
    }
    return currentObj;
}
@end

像这样使用它

NSString* countryCode = [myDict valueForKeyPathWithIndexes:@"customer.contactInfo.phoneNumbers[4].countryCode"];

没有错误检查,因此它很容易损坏,但您明白了。我将此答案交叉发布到类似的(链接的)问题。

Here's a category I wrote for NSObject that can handle array indexes so you can access your nested object like this: "customer.contactInfo.phoneNumbers[4].countryCode"

@interface NSObject (ValueForKeyPathWithIndexes)
   -(id)valueForKeyPathWithIndexes:(NSString*)fullPath;
@end


#import "NSObject+ValueForKeyPathWithIndexes.h"    
@implementation NSObject (ValueForKeyPathWithIndexes)

-(id)valueForKeyPathWithIndexes:(NSString*)fullPath
{
    //quickly use standard valueForKeyPath if no arrays are found
    if ([fullPath rangeOfString:@"["].location == NSNotFound)
        return [self valueForKeyPath:fullPath];

    NSArray* parts = [fullPath componentsSeparatedByString:@"."];
    id currentObj = self;
    for (NSString* part in parts)
    {
        NSRange range = [part rangeOfString:@"["];
        if (range.location == NSNotFound)           
        {
            currentObj = [currentObj valueForKey:part];
        }
        else
        {
            NSString* arrayKey = [part substringToIndex:range.location];
            int index = [[[part substringToIndex:part.length-1] substringFromIndex:range1.location+1] intValue];
            currentObj = [[currentObj valueForKey:arrayKey] objectAtIndex:index];
        }
    }
    return currentObj;
}
@end

Use it like so

NSString* countryCode = [myDict valueForKeyPathWithIndexes:@"customer.contactInfo.phoneNumbers[4].countryCode"];

There's no error checking, so it's prone to breaking but you get the idea. I cross posted this answer to a similar (linked) question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文