开发者

Problem assigning value obtained from [array objectAtIndex:]

In my Piano class, I have a property (Keys is another custom class)

@property (nonatomic, retain) Keys *lastPlayed;

In one of my Piano methods, I set the value of lastPlayed using an object from an array of Key objects.

self.lastPlayed = [allKeys objectAtIndex:variable];

The above line of code causes the progra开发者_开发知识库m to crash.

I've noticed that if I hardcode a specific Key object from the allKeys array, then it works fine. Like so:

self.lastPlayed = keyC;

Interestingly, it doesn't crash if I put the crashing code into a different method.

How can I prevent the crash?

EDIT:

I call this method in the Keys class, where my piano is the delegate

[delegate deliverTagwithNameTag:self.tag]

the piano then responds

- (void) deliverTagwithNameTag:(int)nameTag {

self.lastPlayed = [allKeys objectAtIndex:nameTag];

}


You're probably not getting a plain unexplained crash, you're probably raising an exception. You can watch the console to find out which exception you raised. Normally in this sort of situation it'll be something useful to tell you either that you're asking the array for an out-of-bounds value (which could happen if variable were collecting an incorrect value somehow), that the array itself is invalid (which could be a memory allocation problem) or that the thing returned can't be stored as lastPlayed (which would normally indicate you're doing something custom in the setter and getting an unexpected type of class).

So to prevent the crash, check your console and look for one of those problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜