错误 EXC_BAD_ACCESS
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
保留你的字符串。
myString = [[NSString stringWithFormat:@"%lf",loc.longitude] keep];
然后在此类的 dealloc 方法中(或添加它)
-(void) 释放{
[超级释放];
[myString 发布];
然后
假设您已在 .h 文件中声明了 myString,则应该可以解决此问题
Retain your string.
myString = [[NSString stringWithFormat:@"%lf",loc.longitude] retain];
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.
您是否将 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.