如何使用整数值作为"键"在NSMutableDictionary中设置浮点值?

推荐答案

由于NSDictionary仅设计用于处理对象,因此一种简单的方法是将整数包装并浮在NSNumber对象中.例如:

NSMutableDictionary *testDictionary = [[NSMutableDictionary alloc] init];
[testDictionary setObject:[NSNumber numberWithFloat:1.23f]
                   forKey:[NSNumber numberWithInt:1]];
NSLog(@"Test dictionary: %@", testDictionary);

[testDictionary release];

要提取相关值,只需从NSNumber类中使用适当的intValue、floatValue等方法.

Objective-c相关问答推荐

TWTweetComposeViewController 在 IOS6 中已弃用

UIGestureRecognizer 是否适用于 UIWebView?

改变 RGB colored颜色 的色调

防止其他程序员调用 -init 的最佳方法

dispatch_get_global_queue 和 dispatch_queue_create 有什么区别?

适用于 ios 的 360° 全景库

Select 时如何更改 UITableViewCell 的 colored颜色 ?

在 Xcode 5 中运行代码覆盖时出现数十个profiling:invalid arc tag

以编程方式创建 UITableView

使用 DatePicker 展开和折叠 UITableViewCells

dyld:找不到符号:try 运行 iOS 应用程序时的 _NSURLAuthenticationMethodClientCertificate

访问当前视图的父视图 - iOS

属性与实例变量

Select 时 UITableViewCell 复选标记更改

UIBezierPath 减go 路径

如何逐行从 NSFileHandle 读取数据?

在 UITextField 文本上投影

在 iOS 7 上运行应用程序时显示黑条(4 英寸视网膜显示屏)

捕捉固定步数的 UISlider(如 iOS 7 设置应用程序中的文本大小)

使用正则表达式查找/替换 NSString 中的子字符串