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
并且它显然没有 10 个百万+项目,所以我得到了越界错误。我尝试使用 (int)cardIndex
进行转换,但没有更好的结果。奇怪的是,类似的代码在其他一些类中工作得很好。
这是我想在整个应用程序中使用的一些数据,因此我有一个 Model 类,它在 AppDelegate 中初始化为 theData
,然后由不同的 ViewController 访问。在成功访问其他某个 ViewController(该 ViewController 也执行保留/释放操作)后,会出现此错误。
任何帮助将不胜感激。
I am having a weird problem with variable values. This is the code (it is part of a class method):
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];
I am using the debugger with the objc_exception_throw
breakpoint. The input received by objectAtIndex
in there shows as having value = 13760640. The cards
attribute of appData is an NSArray
and it clearly does not have ten million + items so I get an out of bounds error. I tried casting with (int)cardIndex
with no better results. Weird thing is a similar code in some other class works fine.
This is some data I want to use throughout my app so I have a Model class that gets initialized in the AppDelegate as theData
and then is accesed by different ViewControllers. This error shows up after one successful access on some other ViewController (that one also does retain/release).
Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
[cardIndex unsignedIntValue]
作为objectAtIndex:
行。您不能给
objectAtIndex:
一个指针,因为它需要一个无符号整数。例如:
编辑:
听起来
cardIndex
是一个int
但在某处它被设置为NSNumber
实例。作为一种破解,请使用[(id)cardIndex unsignedIntValue]
。如果这有效,则意味着您使用了错误的cardIndex
类型(它应该是 NSNumber,而不是int
)。Use
[cardIndex unsignedIntValue]
for theobjectAtIndex:
line.You cannot give
objectAtIndex:
a pointer because it expects an unsigned integer.For example:
EDIT:
It sounds like
cardIndex
is anint
but somewhere along the lines it is being set as anNSNumber
instance. As a hack, use[(id)cardIndex unsignedIntValue]
. If this works, then it means you are using the wrong type forcardIndex
(it should be NSNumber, notint
).