将 NSDictionary 的键转换为 NSNumber
如何将密钥转换为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可能是你的顺序错误。
意味着
您可能想要
详细形式的内容:
could be that you have the order wrong.
means
you probably want
which is in verbose form:
上面的行应该是:
The above line should be:
假设您的密钥是 NSString,则无法将密钥传递给 [NSNumber numberWithInteger:key] 因为它不是整数!对键使用 intValue 方法来获取整数,然后将整数转换为 NSNumber,如下所示:
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:
integerValue 返回一个 NSInteger,它不是一个 NSNumber。您应该直接传递 NSNumber:
integerValue returns an NSInteger, which is not an NSNumber. You should pass the NSNumber directly: