gpt4 book ai didi

iphone - 关于-(NSDictionary)dictionaryWithObjectsAndKeys : and

转载 作者:行者123 更新时间:2023-12-03 18:19:11 25 4
gpt4 key购买 nike

我有一个非常有趣的问题。

在我的一个类中,我声明了一个非常简单的实例方法 -(NSDictionary)dictionary;是这样实现的:

- (NSDictionary *)dictionary {
return [NSDictionary dictionaryWithObjectsAndKeys:
self.userID, @"id",
self.userName, @"name",
self.userSurname, @"sName",
self.userNickname, @"nName",
self.userPassword, @"pwd",
[NSNumber numberWithDouble:[self.userBirthday timeIntervalSince1970] * 1000], @"birthday",
self.userSex, @"sex",
self.userEmail, @"email",
self.userLanguage, @"locale",
[NSNumber numberWithLongLong:self.userCoin], @"coin",
self.userRate, @"rate",
[NSNumber numberWithDouble:self.userCoordinate.latitude], @"lat",
[NSNumber numberWithDouble:self.userCoordinate.longitude], @"lon",
self.userPlaces, @"userPlaces",
nil];
}

声明此方法后,我的返回字典中没有 @"userPlaces"键(self.userPlace 显然已被评估并且充满了对象)。

所以我改变了一点我的方法,如下所示:

- (NSDictionary *)dictionary {      
NSMutableDictionary *toReturn = [NSMutableDictionary dictionary];
[toReturn setValue:self.userID forKey:@"id"];
[toReturn setValue:self.userName forKey:@"name"];
[toReturn setValue:self.userSurname forKey:@"sName"];
[toReturn setValue:self.userNickname forKey:@"nName"];
[toReturn setValue:self.userPassword forKey:@"pwd"];
[toReturn setValue:[NSNumber numberWithDouble:[self.userBirthday timeIntervalSince1970] * 1000] forKey:@"birthday"];
[toReturn setValue:self.userSex forKey:@"sex"];
[toReturn setValue:self.userEmail forKey:@"email"];
[toReturn setValue:self.userLanguage forKey:@"locale"];
[toReturn setValue:[NSNumber numberWithLongLong:self.userCoin] forKey:@"coin"];
[toReturn setValue:self.userRate forKey:@"rate"];
[toReturn setValue:[NSNumber numberWithDouble:self.userCoordinate.latitude] forKey:@"lat"];
[toReturn setValue:[NSNumber numberWithDouble:self.userCoordinate.longitude] forKey:@"lon"];
[toReturn setValue:self.userPlaces forKey:@"userPlaces"];

return [NSDictionary dictionaryWithDictionary:toReturn];
}

所有键现在都出现在输出字典中!!!

这个问题让我疯狂地想要理解,但我的问题是......有什么原因导致第二种方法比第一种方法更好?

我没有找到原因。

最佳答案

[NSDictionary DictionaryWithObjectsAndKeys: 当发现 nil 值时停止添加对象。

因此,您尝试在第一个代码中添加的对象很可能是 nil。

关于iphone - 关于-(NSDictionary)dictionaryWithObjectsAndKeys : and,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7515808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com