簡體   English   中英

NSInteger不等於多久?

[英]NSInteger not equal to long?

我正在嘗試在Cocoa中解析一些json數據,但遇到了NSInteger數據類型的麻煩。 json字符串具有一些我分配給NSInteger屬性的長值。 不幸的是,分配的NSInteger值與long值完全不同。 為什么會這樣? NSInteger定義為typedef長的NSInteger。 我可以將long值分配給long屬性,但是我只想知道為什么不能將其分配給NSInteger。

    -(void)parseData:(NSData*)data
{
    NSError*err=nil;
    NSDictionary*jsonData=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
    _userID=(NSInteger)[jsonData valueForKeyWithoutNSNull:@"id"];
}

_userID是NSInteger。 從字典中檢索的值很長。

您不能簡單地將NSNumber (甚至NSString )轉換為NSInteger 字典和其他集合類不能存儲NSInteger類的原始類型。

假設您的字典包含數字而不是字符串,那么您需要:

NSNumber *number = [jsonData valueForKeyWithoutNSNull:@"id"];
_userID = [number integerValue];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM