iphone nslog损坏了数据

时间:2009-08-25 02:02:18

标签: ios iphone nslog corrupt

我对变量值有一个奇怪的问题。这是代码(它是类方法的一部分):

MyAppDelegate *pDelegate = [[UIApplication sharedApplication] delegate];
SomeDictionaryData *appData = [pDelegate.theData retain];
NSLog(@"my instance var: %@",cardIndex); // outputs "my instance var: 4"
NSDictionary *currentCard = [[NSDictionary alloc] initWithDictionary:[appData.cards objectAtIndex:cardIndex]];;
// the above line breaks the app
[currentCard release];
[appData release];

我正在使用带有objc_exception_throw断点的调试器。 objectAtIndex在其中收到的输入显示为值= 13760640.appData的cards属性为NSArray,它显然没有一千万个项目,所以我得到了一个边界错误。我尝试使用(int)cardIndex进行投射而没有更好的结果。奇怪的是,在其他一些类作品中,类似的代码很好。

这是我想在整个应用程序中使用的一些数据,因此我有一个Model类,它在AppDelegate中初始化为theData,然后由不同的ViewControllers加入。在一次成功访问某个其他ViewController(也确实保留/释放)后,会出现此错误。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

[cardIndex unsignedIntValue]行使用objectAtIndex:

你不能给objectAtIndex:指针,因为它需要一个无符号整数。

例如:

NSDictionary *currentCard = [[NSDictionary alloc] initWithDictionary:[appData.cards objectAtIndex:[cardIndex unsignedIntValue]]];

编辑:

听起来cardIndex是一个int,但在某些地方,它被设置为NSNumber个实例。作为黑客,使用[(id)cardIndex unsignedIntValue]。如果这样做,则意味着您使用了错误的cardIndex类型(它应该是NSNumber,而不是int)。

相关问题