gpt4 book ai didi

iphone - 创建 NSDictionary

转载 作者:行者123 更新时间:2023-12-03 18:14:49 27 4
gpt4 key购买 nike

在以下代码中,第一个日志语句按预期显示小数,但第二个日志语句记录 NULL。我做错了什么?

NSDictionary *entry = [[NSDictionary alloc] initWithObjectsAndKeys:
@"x", [NSNumber numberWithDouble:acceleration.x],
@"y", [NSNumber numberWithDouble:acceleration.y],
@"z", [NSNumber numberWithDouble:acceleration.z],
@"date", [NSDate date],
nil];
NSLog([NSString stringWithFormat:@"%@", [NSNumber numberWithDouble:acceleration.x]]);
NSLog([NSString stringWithFormat:@"%@", [entry objectForKey:@"x"]]);

最佳答案

您正在交换插入对象和键的顺序:您需要先插入对象,然后插入键,如下例所示。

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];

关于iphone - 创建 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1107996/

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