iphone nslog 数据损坏

发布于 2024-08-02 08:05:38 字数 902 浏览 9 评论 0原文

我在变量值方面遇到了一个奇怪的问题。这是代码(它是类方法的一部分):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若相惜即相离 2024-08-09 08:05:38

使用 [cardIndex unsignedIntValue] 作为 objectAtIndex: 行。

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

例如:

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

编辑:

听起来 cardIndex 是一个 int 但在某处它被设置为 NSNumber 实例。作为一种破解,请使用[(id)cardIndex unsignedIntValue]。如果这有效,则意味着您使用了错误的 cardIndex 类型(它应该是 NSNumber,而不是 int)。

Use [cardIndex unsignedIntValue] for the objectAtIndex: line.

You cannot give objectAtIndex: a pointer because it expects an unsigned integer.

For example:

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

EDIT:

It sounds like cardIndex is an int but somewhere along the lines it is being set as an NSNumber instance. As a hack, use [(id)cardIndex unsignedIntValue]. If this works, then it means you are using the wrong type for cardIndex (it should be NSNumber, not int).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文