错误:在NSMutableArray上使用addObjects:时出现“无法识别的选择器”

时间:2011-07-01 17:37:37

标签: objective-c properties nsmutablearray nsarray

执行时

[self.blockViews addObject:curBlockView];

我收到错误

  

2011-07-01 13:35:26.240 Block Breaker [42061:207] - [__ NSArrayI addObject:]:无法识别的选择器发送到实例0x4e037a0

我对Objective-C很新。这是我的init方法吗?

//
//  GameEngine.h
//  Block Breaker
//
//  Created by Chris Muench on 7/1/11.
//  Copyright 2011 N/A. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface GameEngine : NSObject {
    NSMutableArray *blockViews;
    int numBlockRows;
    int score;
}
@property (nonatomic, copy) NSMutableArray *blockViews;
@property int numBlockRows;
@property int score;

- (void) setup;
- (void) setupBlocks;
@end


//
//  GameEngine.m
//  Block Breaker
//
//  Created by Chris Muench on 7/1/11.
//  Copyright 2011 N/A. All rights reserved.
//

#import "GameEngine.h"
#import "Block.h"
#import "BlockView.h"


@implementation GameEngine
@synthesize blockViews;
@synthesize numBlockRows;
@synthesize score;

- (id) init
{
    if ((self = [super init])) 
    {
        self.blockViews = [[NSMutableArray alloc] init];
        self.numBlockRows = 2;
        self.score = 0;
    }
    return self;
}

- (void) setup
{
    [self setupBlocks];
}

- (void) setupBlocks
{
    float blockWidth = 10;
    float blockHeight = 10;
    float rowSpacing = 2;
    float colSpacing = 2;
    float currentX = 0;
    float currentY=10;
    float screenWidth = 200;

    for (int rowCounter=0;rowCounter<self.numBlockRows;rowCounter++)
    {

        while(currentX <=screenWidth)
        {
            Block *curBlock = [[Block alloc] initWithWidth:blockWidth height:blockHeight];
            BlockView *curBlockView = [[BlockView alloc] initWithFrame:CGRectMake(currentX, currentY, curBlock.width, curBlock.height)];
            curBlockView.block = curBlock;

            [self.blockViews addObject:curBlockView];           
            currentX+=blockWidth+colSpacing;
            [curBlock release];
            [curBlockView release];
        }

        currentX=0;
        currentY+=blockHeight+rowSpacing;
    }


}

@end

3 个答案:

答案 0 :(得分:10)

使用NSMutableArray方法复制copy或将属性指定为copy的合成设置器时,您将获得不可变副本,这意味着你最终会得到一个简单的NSArray。*有一个方法mutableCopy将保留可变性,但我不相信有任何方法可以为属性指定。

如果您希望在设置数组时可变地复制数组,则必须编写自己的setter方法,并在属性声明中指定该方法。

@property (nonatomic, copy, setter=setBlockViewsByCopying) NSMutableArray * blockViews;

- (void) setBlockViewsByCopying: (NSMutableArray *)newBlockViews {
    NSMutableArray * tmp = [newBlockViews mutableCopy];
    [blockViews release];
    blockViews = tmp;
}

旁注,正如评论中提到的@InsertWittyName,您的代码初始化blockViews会产生泄漏,因为您在创建的阵列上有两个所有权声明 - 一个用于{{1您使用该属性获得allocretain的一个:

copy

你应该这样做:

self.blockViews = [[NSMutableArray alloc] init];
//  ^ One claim                    ^ Two claims
// Only one release later, when the property is set again!
// LEAK!

*虽然它是一个类集群,但你真的得到了一些未指定的具体子类。

答案 1 :(得分:0)

如果我没记错的话,副本应该保留。

无论如何使用复制都不是最佳的,每次调用访问器(set)时,你都有一个副本,并且,如上所述,它是不可变的。

添加很好,但记得在完成后释放数组。

答案 2 :(得分:-1)

你的阵列是retain吗?