错误 EXC_BAD_ACCESS

发布于 2024-09-11 18:15:26 字数 445 浏览 2 评论 0原文

我是 iPhone 开发新手,遇到 EXC_BAD_ACCESS 错误。

在我的 .h 文件中,我声明:

NSString *myString;

在我的 .m 文件中,我有两种方法:

1.CLLocationManager 接口的 (void)locationManager,当我执行此操作时:

myString = [NSString stringWithFormat:@"%lf",loc.longitude];
NSLog(@"%@",myString); // it works

2.A (void)sendPosition 方法:

NSLog(@"%@",myString); // EXC_BAD_ACCESS

你能帮我吗?

I am new in iPhone developement and I have a EXC_BAD_ACCESS error.

In my .h file I declare:

NSString *myString;

In my .m file, I have two methods:

1.The (void)locationManager of CLLocationManager interface when I do it:

myString = [NSString stringWithFormat:@"%lf",loc.longitude];
NSLog(@"%@",myString); // it works

2.A (void)sendPosition method with:

NSLog(@"%@",myString); // EXC_BAD_ACCESS

Can you help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

总以为 2024-09-18 18:15:26
  1. 保留你的字符串。

    myString = [[NSString stringWithFormat:@"%lf",loc.longitude] keep];

  2. 然后在此类的 dealloc 方法中(或添加它)

    -(void) 释放{
    [超级释放];
    [myString 发布];
    然后

假设您已在 .h 文件中声明了 myString,则应该可以解决此问题

  1. Retain your string.

    myString = [[NSString stringWithFormat:@"%lf",loc.longitude] retain];

  2. Then in your dealloc method of this class (or add it)

    -(void) dealloc{
    [super dealloc];
    [myString release];
    }

that should fix it, assuming you have declared myString in you .h file.

白芷 2024-09-18 18:15:26

您是否将 myString 保留在 locationManager 方法中的其他位置?如果没有,您应该在 -dealloc 方法中释放它。否则,它可能会消失,就像我怀疑现在正在发生的事情一样。请阅读内存管理规则< /a>.

我建议创建一个属性,该属性会在您的类上自动保留该值,然后在创建字符串时用点符号分配它。再次,在 dealloc 方法中释放它。如今,这对于此类代码来说是惯用的。

Are you retaining myString somewhere else in your locationManager method? If not, you should, then release it in your -dealloc method. Otherwise, it could disappear like I suspect is what's happening now. Please read up on the rules for memory management.

What I would recommend is creating a property which retains the value automatically on your class, then assign it with the dot notation when you create the string. Again, releasing it in your dealloc method. This is idiomatic these days for this type of code.

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