1

我有一个名为“CardSet”的类,其中包含一个 NSMutableArray* cardSet 来保存“卡片”,我将其扩展为“DeckCards”。我希望“CardSet”有一个名为“(void)addCard:(Card*)”的方法(以及类似的方法“removeCard”)。我想要“addCard”来访问和设置cardSet。更好的是我想使用“addCard”方法来初始化cardSet。类文件“CardSet.h”内容如下:

 #import < Cocoa/Cocoa.h >

 #import < Card.h >

@interface CardSet : NSObject {

    NSMutableArray* cardSet;

}

-(id)init;

-(NSMutableArray*)getCardSet;

-(void)setCardSet:(NSMutableArray *)new_cardset;

-(Card*)getCard:(NSInteger) index;

**-(void)addCard:(Card*) new_card;**

-(void)removeCard:(Card*) old_card;

-(void)dealloc;

@property (readwrite, retain, getter=getCardSet, setter=setCardSet) NSMutableArray* cardSet;

@end

方法文件内容如下:

 #import "CardSet.h"

@implementation CardSet

-(id)init{
    if( self = [super init] ){}   //will add initialisations here later
    return self;
}

-(NSMutableArray*)getCardSet{
    return cardSet;
}

-(void)setCardSet:(NSMutableArray *)new_cardSet{
    cardSet = new_cardSet;
}

-(Card*)getCard:(NSInteger)index{
    return [cardSet objectAtIndex:index];
}

**-(void)addCard:(Card *)new_card{
    [cardSet addObject:new_card];
}**

-(void)removeCard:(Card *)old_card{
    [cardSet removeObject:old_card];
}

-(void)dealloc{
    [cardSet release];
    [super dealloc];
}

@synthesize cardSet;

@end

这编译得很好。我想使用它的“addCard”方法初始化一个“DeckCards”实例 52 次。当我在 DeckCards setter 方法中调用 addCard 52 次并询问其“cardSet”的大小时,我返回 0。

这似乎是范围或权限问题?“addCard”方法可以有任何setter权限吗?setter 参数必须与返回值和相应的成员类型相同吗?

[我可以通过在“DeckCard”之外创建一个 NSMutableArray 对象“deck_cards_temp”来解决上述问题,向其中添加 52 张卡片,然后通过从“CardSet”继承的设置器将其传递给我的“DeckCards”实例的成员。这不是很满意!]

你有什么建议?非常感谢您的帮助和耐心。

4

3 回答 3

3

您永远不会真正创建cardSet对象。您应该在您的-init方法中创建它:

-(id)init
{
    if( self = [super init] )
    {
        cardSet = [[NSMutableArray alloc] init];
    }
    return self;
}

因为您从未实际创建数组,所以所有对的调用-addCard:都被发送到一个 nil 对象。

当您将数组传递给 时-setCardSet:,您将传入一个初始化的数组,因此该数组不再为 nil 并且-addCard:调用工作正常。

于 2010-03-01T01:53:41.773 回答
0

CardSet.h

#import <Cocoa/Cocoa.h>

// For know we just need to know there is a class named "Card" being used but implemented later
@class Card;

@interface CardSet : NSObject {
    NSMutableArray *cardSet;
}

// Here are the methods according to "correct" naming conventions
- (Card *)cardAtIndex:(NSInteger)index;
- (void)addCard:(Card *)card;
- (void)removeCard:(Card *)card;

// This will help us and forget about writing the setter/getter
@property (nonatomic, retain) NSMutableArray *cardSet;

@end

CardSet.m

#import "CardSet.h"
// Now we tell the compiler what "Card" is and what methods etc. it has
#import "Card.h"

@implementation CardSet

@synthesize cardSet;

- (id)init {
    if (self = [super init]) {
        // If we don't create the cardSet, how are we able to work with it!?
        NSMutableArray *anArray = [[NSMutableArray alloc] init];
        self.cardSet = anArray;
        [anArray release];
    }
    return self;
}

- (Card *)cardAtIndex:(NSInteger)index {
    return [cardSet objectAtIndex:index];
}

- (void)addCard:(Card *)card {
    [cardSet addObject:card];
}

- (void)removeCard:(Card *)card {
    [cardSet removeObject:card];
}

- (void)dealloc {
    [cardSet release];
    [super dealloc];
}

@end

正如Abizern已经指出的那样:将数组命名为与您的类相同是一件坏事。

于 2010-03-01T13:23:37.713 回答
0

我会缩短那个初始化方法:

- (id)init {
    if (self = [super init]) {
        // If we don't create the cardSet, how are we able to work with it!?
        self.cardSet = [NSMutableArray array];
    }
    return self;
}
于 2010-03-01T16:33:41.517 回答