糟糕的 UIViewController
我有一个 viewController 给我带来了问题...
UIViewController *nextController = [[NextView alloc] initWithNibName:@"NextView" bundle:nil];
[currentPageController.view removeFromSuperview];
[self.view addSubview:nextController.view];
我的应用程序因 EXC_BAD_ACCESS 崩溃。
有人知道什么可能会导致这种情况吗?
提前致谢!
更新
使用断点并逐步执行代码后,问题似乎出在我的 viewController 的 viewDidLoad 中的这段代码:
NSString *noteToSet;
if ([Settings isData]) {
noteToSet = [NSString stringWithFormat:@"Data, "];
}
if ([Settings isGeom]) {
if ([noteToSet isEqualToString:@""]) {
noteToSet = [NSString stringWithFormat:@"Geom, "];
} else {
noteToSet = [noteToSet stringByAppendingFormat:@"Geom, "];
}
}
有人看到那里有问题吗? 非常感谢!
已修复
通过使用空白值@""初始化字符串来修复它
noteToSet = [NSString stringWithFormat:@""];
I have ONE viewController that is giving me a problem...
UIViewController *nextController = [[NextView alloc] initWithNibName:@"NextView" bundle:nil];
[currentPageController.view removeFromSuperview];
[self.view addSubview:nextController.view];
My app crashes here with an EXC_BAD_ACCESS.
Does anybody have ANY idea what could cause this?
Thanks in advance!
UPDATE
After using Breakpoints and stepping through the code, the problem seems to be with this bit of code in the viewDidLoad of my viewController:
NSString *noteToSet;
if ([Settings isData]) {
noteToSet = [NSString stringWithFormat:@"Data, "];
}
if ([Settings isGeom]) {
if ([noteToSet isEqualToString:@""]) {
noteToSet = [NSString stringWithFormat:@"Geom, "];
} else {
noteToSet = [noteToSet stringByAppendingFormat:@"Geom, "];
}
}
Anybody see a problem there?
Thanks so much!
FIXED
Fixed it by initializing the string with the blank value @""
noteToSet = [NSString stringWithFormat:@""];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所以答案的第一部分是 - 如果你的 viewController 无法加载并且你不知道为什么 - 检查 viewDidLoad 中的代码,这就是我的问题所在,它让我疯狂地试图找出 viewController 本身的问题当它实际上是 viewDidLoad 中的 NSString 问题时。
第二部分是,您不能使用 [stringName isEqualToString:@""] 将 NSString 与空白值进行比较,除非您从 NSUSerDefaults 获取该字符串,或者除非您首先将该字符串设置为等于 @""。
So the first part of the answer is - if your viewController won't load and you have no idea why - check the code in viewDidLoad, that's where my issue was and it drove me crazy trying to figure out what was wrong with the viewController itself when it was really an NSString issue in the viewDidLoad all along.
The second part is that you can't compare an NSString to a blank value using [stringName isEqualToString:@""] unless you got that string from NSUSerDefaults or unless you first set the string to be equal to @"".
我在发布的代码中没有看到任何会导致异常的内容。但是,您发布的两段代码都包含以下行:
由于第一行使 currentPageController 指向与 nextController 相同的对象,因此第二行和第三行相互抵消。你不妨这样写:
就这样吧。代码中其他地方的类似误解很容易导致您过于频繁地错过保留或释放,并导致您似乎看到的那种错误指针。
I don't see anything in the posted code that'd cause the exception. However, both pieces of code that you posted contain the lines:
Since the first line makes currentPageController point to the same object as nextController, the second and third lines cancel each other out. You might as well write:
and leave it at that. A similar misunderstanding at some other point in the code could easily cause you to miss a retain or release once too often and cause the sort of bad pointer that you seem to be seeing.
EXC_BAD_ACCESS 通常是由内存管理不善引起的。转到 Xcode 中的“构建”菜单并使用“分配”对其进行分析(在模拟器中)。然后进去并确保您已启用僵尸。在模拟器中运行应用程序并将其指向出现错误的位置。然后,仪器应该告诉您内存管理问题在哪里。如果您仍然无法得到它,请告诉我们您得到了什么。
这是指南: http://www.markj.net/iphone-memory-debug-僵尸僵尸/
EXC_BAD_ACCESS is often caused by poor memory management. Go to the Build Menu in Xcode and Profile it (in the simulator) using Allocations. Then go in and make sure you have Zombies Enabled. Run the app in the simulator and point it to where you get the error. Instruments should then tell you where the bad memory management is. If you still can't get it, then tell us what you're getting.
Here's a guide: http://www.markj.net/iphone-memory-debug-nszombie/