2009-08-25 187 views
1

我有一个变量值奇怪的问题。这是该代码(这是一个类方法的一部分):iphone nslog损坏的数据

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,它显然没有1000万以上的项目,所以我得到了一个超出界限的错误。我试着用(int)cardIndex铸造没有更好的结果。奇怪的是一些其他类中的类似代码很好。

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

任何帮助将不胜感激。

回答

0

使用[cardIndex unsignedIntValue]objectAtIndex:行。

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

例如:

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

编辑:

这听起来像cardIndexint但某处沿着线它被设置为NSNumber实例。作为黑客,请使用[(id)cardIndex unsignedIntValue]。如果这有效,那么这意味着你使用了cardIndex的错误类型(它应该是NSNumber,而不是int)。

+0

谢谢兄弟!虽然我得到一个“无效的接收器类型int”警告。 – mga 2009-08-25 02:44:27

+0

尝试投射为(NSUInteger)并仍然收到警告。其他一切正常,但...我只是不喜欢警告。 – mga 2009-08-25 02:45:30

+0

对!将所有必要的引用转换为NSNumber。谢谢! – mga 2009-08-25 03:45:14