将 NSDictionary 的键转换为 NSNumber

发布于 2024-11-25 01:55:52 字数 1179 浏览 0 评论 0原文

如何将密钥转换为 NSNumber?这段代码给了我错误:

无论我如何尝试,我总是会遇到错误:从“NSIntegar”(又名“int”)分配给“NSNumber”的整数到指针转换不兼容。

        for (id key in consultants)
    {
        consultantData = [[ConsultantData alloc] init];

        consultantData.name = [consultants objectForKey:key];
        consultantData.conID = [[NSNumber numberWithInteger:key] integerValue];

        NSLog(@"name: %@    ID: %@", consultantData.name, consultantData.conID);

        [consultantList addObject:consultantData];
        [consultantData release];
    }

这是我的对象 ConsultantData:

#import <Foundation/Foundation.h>

@interface ConsultantData : NSObject 
{
    NSString *name;
    NSNumber *conID;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *conID;

@end

#import "ConsultantData.h"


@implementation ConsultantData

@synthesize name;
@synthesize conID;

-(void) dealloc
{
    [name release];
    [conID release];

    [super dealloc];
}

@end

如果我尝试 isMemberOfClass,它永远不会返回 true。如果我尝试 isKindOfClass,它告诉我它是一个 NSString。

Consultants 是我从服务器收到的字典,它通过 XMLRPC 函数(服务器是 PHP)。据我所见,一切都是服务器字典返回的字符串。

How can i convert the key to an NSNumber? this code gives me the error:

no matter how i try it, i always end up with the error: Incompatible integer to pointer conversion assigning to 'NSNumber' from 'NSIntegar' (aka 'int').

        for (id key in consultants)
    {
        consultantData = [[ConsultantData alloc] init];

        consultantData.name = [consultants objectForKey:key];
        consultantData.conID = [[NSNumber numberWithInteger:key] integerValue];

        NSLog(@"name: %@    ID: %@", consultantData.name, consultantData.conID);

        [consultantList addObject:consultantData];
        [consultantData release];
    }

here is my object ConsultantData:

#import <Foundation/Foundation.h>

@interface ConsultantData : NSObject 
{
    NSString *name;
    NSNumber *conID;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *conID;

@end

#import "ConsultantData.h"


@implementation ConsultantData

@synthesize name;
@synthesize conID;

-(void) dealloc
{
    [name release];
    [conID release];

    [super dealloc];
}

@end

if i try isMemberOfClass, it never returns true. if i try isKindOfClass, it tells me its a NSString.

consultants is a dictionary i receive from my sever that passes thru an XMLRPC function (server is PHP). from what i've seen, everything is a string in the server dictionary returns.

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

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

发布评论

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

评论(4

漆黑的白昼 2024-12-02 01:55:52

可能是你的顺序错误。

consultantData.conID = [[NSNumber numberWithInteger:key] integerValue];

意味着

NSNumber *foo = [NSNumber numberWithInteger:key];
NSInteger bar = [foo integerValue];
consultantData.conID = bar;

您可能想要

consultantData.conID = [NSNumber numberWithInteger:[key integerValue]];

详细形式的内容:

NSInteger foo = [key integerValue];
NSNumber *bar = [NSNumber numberWithInteger:foo];
consultantData.conID = bar;

could be that you have the order wrong.

consultantData.conID = [[NSNumber numberWithInteger:key] integerValue];

means

NSNumber *foo = [NSNumber numberWithInteger:key];
NSInteger bar = [foo integerValue];
consultantData.conID = bar;

you probably want

consultantData.conID = [NSNumber numberWithInteger:[key integerValue]];

which is in verbose form:

NSInteger foo = [key integerValue];
NSNumber *bar = [NSNumber numberWithInteger:foo];
consultantData.conID = bar;
南薇 2024-12-02 01:55:52

consultantData.conID = [[NSNumber numberWithInteger:key] integerValue];

上面的行应该是:

consultantData.conID = [NSNumber numberWithInteger:[key integerValue]];

consultantData.conID = [[NSNumber numberWithInteger:key] integerValue];

The above line should be:

consultantData.conID = [NSNumber numberWithInteger:[key integerValue]];
北城半夏 2024-12-02 01:55:52

假设您的密钥是 NSString,则无法将密钥传递给 [NSNumber numberWithInteger:key] 因为它不是整数!对键使用 intValue 方法来获取整数,然后将整数转换为 NSNumber,如下所示:

consultantData.conID = [NSNumber numberWithInteger:[key intValue]];

Assuming your key is an NSString, you can't pass the key to [NSNumber numberWithInteger:key] because it is not an integer! Use the intValue method on the key to get an integer, then convert the integer to a NSNumber like so:

consultantData.conID = [NSNumber numberWithInteger:[key intValue]];
月野兔 2024-12-02 01:55:52

integerValue 返回一个 NSInteger,它不是一个 NSNumber。您应该直接传递 NSNumber:

consultantData.conID = [NSNumber numberWithInteger:[key integerValue]];

integerValue returns an NSInteger, which is not an NSNumber. You should pass the NSNumber directly:

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