创建NSDictionary [英] Creating an NSDictionary

查看:147
本文介绍了创建NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码中,第一个日志语句按预期显示小数,但第二个日志为NULL。

In the following code, the first log statement shows a decimal as expected, but the second logs NULL. What am I doing wrong?

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"]]);


推荐答案

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

You are exchanging the order in which you insert objects and key: you need to insert first the object, then the key as shown in the following example.

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

这篇关于创建NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆