1

我刚刚减少了很多错误,现在我的代码编译得很好,但是它总是在我创建的 createEntity 方法中崩溃。它似乎没有任何问题,但无论如何有人可以帮助我吗?有什么建议吗?

 -(void)createEntityWithX:(int)newEntityX andY:(int)newEntityY withType:(int)newEntityType withWidth:(int)newEntityWidth andLength:(int)newEntityLength atSpeed:(int)newEntitySpeed
{
  Entity tmpEntity;
  tmpEntity.entityX = newEntityX;
  tmpEntity.entityY = newEntityY;
  tmpEntity.entityLength = newEntityLength;
  tmpEntity.entityWidth = newEntityWidth;
  tmpEntity.entityType = newEntityType;
  tmpEntity.entitySpeed = newEntitySpeed;

  int arrayAmount = [entityArray count];
  NSValue *tmp = [NSValue valueWithBytes:&tmpEntity objCType:@encode(struct Entity)];

  [entityArray insertObject:tmp atIndex:arrayAmount];

  [tmp release];
}
4

1 回答 1

1

这里有两件事之一或两者都是错误的。可能entityArray不是NSMutableArray,而是NSArray,在这种情况下,您会收到“无法识别的选择器”错误。

修复后,您需要修复索引。您不能对象插入NSMutableArray到与其对应的索引处count,因为插入的最后一个有效索引是count-1。您应该使用addObject:在数组末尾放置一些东西:

NSValue *tmp = [NSValue valueWithBytes:&tmpEntity objCType:@encode(struct Entity)];

[entityArray addObject:tmp];
于 2012-05-05T20:07:02.417 回答