【问题标题】:Return value For NSDictionary On Simulator, iPhoneNSDictionary On Simulator, iPhone 的返回值
【发布时间】:2011-01-13 13:25:52
【问题描述】:
我有一些代码需要 iPhone 才能运行。但是,我确实想在模拟器上测试我的应用程序。在 iPhone 上,我使用它来返回一个值:
return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:-1], @"number"];
我正在寻找类似的东西我认为:
- (NSDictionary *) data
{
#if TARGET_IPHONE_SIMULATOR
something in here to return a value of 70;
#else .....
我想返回一个值 70 以在模拟器中使用。
有人可以帮我吗?
【问题讨论】:
标签:
iphone
objective-c
ios-simulator
nsdictionary
【解决方案1】:
始终使用nil 终止您的+dictionaryWithObjectsAndKeys:,否则您会随机崩溃。如果只有 1 个密钥,最好使用+dictionaryWithObject:forKey:。
使用#else。
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:
#if TARGET_IPHONE_SIMULATOR
70
#else
-1
#endif
], @"number", nil];
或者,更简洁,
return [NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:(TARGET_IPHONE_SIMULATOR ? 70 : -1)]
forKey:@"number"];