English 中文(简体)
EXC_BAD_ACACEES 在 xcode 中试图从其他类中调用一种方法
原标题:EXC_BAD_ACCESS in xcode trying to call a method from another class

嗨,我碰见ExC_BAD的 适应在我的代码,

I m trying to run a method in class PurchaseTowerMenuLayer, method is from class UnitDatabase:

购买MenuLayer.m:

delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// getUnitDatabase returns UnitDatabase class from GameScene class

[[[delegate getGameScene] getUnitDatabase] createUnit:@"t01"]; //EXC_BAD_ACCESS happens here

UnitedDatabase.m: 单位数据库: m:

-(void)createUnit:(NSString *)unitID
{

typeString = [unitID substringWithRange:NSMakeRange(0,1)];
numberString = [unitID substringWithRange:NSMakeRange(1,2)];

Unit *unit;

if(unitID == @"t00")
    [unit setUnitID:unitID];

etc...
}

我在做什么才能弄出这个错误?

提前感谢!

最佳回答

好吧, 首先分隔调用 < code> [[ [远程获取 GameSceen] 获得 UUUDDatabase] 创建 Unit: @ t01] ; 三个行的 , 例如

GameScene* scene = [delegate getGameScene];
UnitDatabase* unitDatabase = [scene getUnitDatabase];
[unitDatabase createUnit:@"t01"];

这将允许您指定哪个呼叫发出 EXC_BAD_ACCESS , 如果您愿意的话, 您可以在后退 。

在您确定哪个行会发出错误后, 它通常是一个查找您没有给 [[MyClass alloc] init] 打电话的事宜; 。 例如, 如果 getGameScene get UnitedDatabase 返回天体, 而这些天体不是 alloc d/ > d > (或 delegate too for that ), 那么您至少要在返回它们之前确保这样做 。

如果看不到更多代码,我帮不了更多忙,但这些错误通常会这样。确保您将您保存的物体作为其它对象的成员进行分解和初始化。

问题回答

看来您还没有分配或初始化 unit 。 请尝试将 < code> United * unit; 更改为 :

unit *unit = [[unitalloc] init];

添加,当然,如果您不使用 ARC,请确保释放/释放/释放。

另外,作为一个未来参考,当你遇到类似尝试时, 尝试识别给您带来错误的具体代码行。 我非常确定问题不是您的 < code> PurchaseTowerMenuLayer.m 的问题, 而是在 < code > 创建 United 方法中的问题。 即便这可能不够准确, 也可能是系统呼叫深十层。 您在请求帮助之前应该尝试识别类似的东西, 在许多情况下, 它会帮助您自己找到答案, 并且会更快。

编辑您的方案, 并启用 NNSZombie 以发现更明确的错误





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签