从Objective-C iPhone dev的plist中获取整数
我有以下DataAll.plist:从Objective-C iPhone dev的plist中获取整数,objective-c,plist,Objective C,Plist,我有以下DataAll.plist: <array> <dict> <key>ID</key> <integer>1</integer> <key>Name</key> <string>Inigo Montoya</string> </dict> .... </array>
<array>
<dict>
<key>ID</key>
<integer>1</integer>
<key>Name</key>
<string>Inigo Montoya</string>
</dict>
....
</array>
这是我的数据
@interface Data : NSObject {
NSNumber *EXID;
NSString *EXName;
}
@property (readwrite, retain) NSNumber *EXID;
@property (nonatomic, retain) NSString *EXName;
和数据
@implementation Data
@synthesize EXID;
@synthesize EXName;
- (void) setData: (NSDictionary *) dictionary {
self.EXName = [dictionary objectForKey:@"Name"];
NSNumber *ID = [dictionary objectForKey:@"ID"];
self.EXID = ID;
}
非常感谢!这是我的第一篇文章,如果格式不正确,我深表歉意。仅仅因为您的数据在plist中是一个整数,并不意味着它是作为一个整数检索的。至少在使用
[dict objectForKey:
检索时不会。此外,NSNumber
和integer
不是相同的数据类型
我猜是后者把你甩了。也就是说隐式转换为NSNumber
。在调用setData
并使用输出更新OP之前,请添加以下内容
NSLog(@"plist entry: %@", first);
仅仅因为您的数据在plist中是一个整数,并不意味着它是作为一个整数检索的。至少在使用
[dict objectForKey:
检索时不会。此外,NSNumber
和integer
不是相同的数据类型
我猜是后者把你甩了。也就是说隐式转换为NSNumber
。在调用setData
并使用输出更新OP之前,请添加以下内容
NSLog(@"plist entry: %@", first);
问题在于
[NSString stringWithFormat:@"%d",[[firstData EXID]];
EXID是一个NSNumber*
对象,而不是int
,因此%d不是您想要的。您需要将%d替换为%@或通过调用[EXID integerValue]
或[EXID intValue]
从NSNumber中展开整数。分别返回NSInteger
和int
。问题出在
[NSString stringWithFormat:@"%d",[[firstData EXID]];
EXID是一个
NSNumber*
对象,而不是int
,因此%d不是您想要的。您需要将%d替换为%@或通过调用[EXID integerValue]
或[EXID intValue]
从NSNumber中展开整数。它们分别返回NSInteger
和int
。plist条目:{ID=1;Name=“Inigo Montoya;”}这说明了什么?谢谢您的快速回复,顺便说一句,@wm_eddie已经确定了确切的问题。我的建议是更改EXID,将其设置为NSInteger
或简单地设置为int
.plist条目:{ID=1;Name=“Inigo Montoya;”}。这说明了什么?谢谢您的快速回复,顺便说一句,@wm_eddie已经确定了确切的问题。我的建议是更改EXID
,将其设置为NSInteger
或干脆设置为int
。非常感谢wm_eddie!%@确实有效(duh),但我也接受了你的建议,选择了NSInteger而不是NSNumber*。非常感谢wm_eddie!%@确实有效(duh),但我也接受了你的建议,选择了NSInteger而不是NSNumber*。