关于 -(NSDictionary)dictionaryWithObjectsAndKeys:和

发布于 2024-12-06 08:36:34 字数 2561 浏览 2 评论 0原文

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

在我的一个类中,我声明了一个非常简单的实例方法 -(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];
}

所有密钥现在都存在于输出字典中!

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

我没有找到原因。

I got a really interesting question.

Inside one of my classes I declared a very simple instance method -(NSDictionary)dictionary;
that is implemented in this way:

- (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];
    }

with this method declared there are no @"userPlaces" key inside my return dictionary (self.userPlace is obviously valorized and full of objects).

So I changed a little bit my method like this:

- (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];
}

All the key are now present inside the output dictionary!!!

This problem drove me crazy to understand but my question is ...
There is any reason couse the second method works better then the first one??

I didn't find a reason for it.

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

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

发布评论

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

评论(2

冬天旳寂寞 2024-12-13 08:36:34

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

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

[NSDictionary dictionaryWithObjectsAndKeys: stops to add objects when it finds a nil value.

So most likely one of the objects you try to add in the first code is nil.

甜嗑 2024-12-13 08:36:34

类似于下面的代码的东西解决了我的问题,但有同样的例外:

(NSMutableDictionary *)dictionary {
return [NSMutableDictionary 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];
}

The something similar to the code below solved my problem with the same exception:

(NSMutableDictionary *)dictionary {
return [NSMutableDictionary 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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文