objective-c - 如何在数组对象中使用 objectAtIndex

标签 objective-c cocoa nsmutablearray

我正在尝试掌握 Objective C 和 Cocoa,所以我可能在这里使用了错误的术语。

我为一副纸牌创建了一个 Objective C 类,由我的主 AppDelegate.h 和 AppDelegate.m 读取,它有两个方法,deckOfCards 和 pickACard。 DeckOfCards 只是一个 NSMutableArray,其中每种卡类型都以字符串形式写出,然后在 pickACard 中我创建一个如下所示的新数组:

-(void)pickACard
{
    DeckOfCards *newDeck = [[DeckOfCards alloc] init];
    int r = arc4random() % 52;
    NSLog (@"The card you picked is: %@, and there are %i cards left", [newDeck     objectAtIndex:r], [newDeck count]);
    [newDeck removeObjectAtIndex:r];
}

但是 XCode 说我无法在这个新数组上使用 objectAtIndex 和 removeObjectAtIndex,因此我无法随机选择一张卡,然后通过删除数组的该部分来“将其从包中删除”。

当它全部在 DeckOfCards 中时,这确实有效,但是当 AppDelegate.m 调用它时,它会创建一个新数组,这样我就可以获得不同的卡片,但永远不会从包中删除多于一张。

我猜我没有正确创建这个新数组。

为了更清楚起见,DeckOfCards.h 是这样的:

#import <Foundation/Foundation.h>

@interface DeckOfCards : NSObject
{
@private
}
-(void) deckOfCards;
-(void) pickACard;

@end

DeckOfCards.m 是这样的:

@implementation DeckOfCards
-(void)deckOfCards
{
NSMutableArray *deckOfCards = [NSMutableArray arrayWithObjects:
    @"One of Hearts", @"Two of Hearts"..., nil];
}
-(void)pickACard
{
    DeckOfCards *newDeck = [[DeckOfCards alloc] init];
    int r = arc4random() % 52;
    NSLog (@"The card you picked is: %@, and there are %i cards left",[newDeck     objectAtIndex:r], [newDeck count]);
    [newDeck removeObjectAtIndex:r];
}

@end

最佳答案

我在这里发现了一些问题。编译器问题的直接原因是您尝试在 DeckOfCards 实例上调用 NSMutableArray 方法。相反,您希望在方法 -[DeckOfCards DeckOfCards] 返回的 NSMutableArray 上调用这些方法。它看起来像这样:

DeckOfCards *newDeck = [[DeckOfCards alloc] init];
NSMutableArray *deckArray = [newDeck deckOfCards];
int r = arc4random() % 52;
NSLog (@"The card you picked is: %@, and there are %i cards left",[deckArray objectAtIndex:r], [deckArray count]);
[deckArray removeObjectAtIndex:r];

但这引出了我们的下一个问题 - -deckOfCards 实际上并不返回任何内容 - 它的返回类型被定义为 void。因此,为了将该 NSMutableArray 实例返回到程序的其余部分,您必须返回它,并使用适当的返回类型定义该方法。如果我们只是为了解决这个问题,我们会更改您的 -deckOfCards 方法,如下所示:

-(NSMutableArray *)deckOfCards
{
    NSMutableArray *deckOfCards = [NSMutableArray arrayWithObjects:
        @"One of Hearts", @"Two of Hearts"..., nil];

    return deckOfCards;
}

好的,现在应该可以编译了。但这里有一些严重的错误。恐怕我现在没有太多的时间来回答这个问题,但我建议你阅读一些 Objective-C 的介绍性内容,重点关注对象和类之间的区别,以及对象的生命周期和内存管理。本质上,没有什么是持久的。无论您在 DeckOfCards 的任何实例上调用 -pickACard 多少次,您总是会实例化一个单独的实例,然后该实例将获得一个全新的 NSMutableArray实例化。但这些对象都不会被保留(假设您正在使用自动引用计数),并且它们都会立即消失。因此,您每次都会从 -pickACard 获得一套全新的 52 张牌。

希望这有帮助。通过花一些时间阅读和学习 Objective-C 和面向对象编程的基础知识,您将能够很好地使该程序按照您的意愿运行。如果您有疑问,请告诉我,我会尽力回答。

关于objective-c - 如何在数组对象中使用 objectAtIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12229248/

相关文章:

objective-c - 在 Swift 类中使用类型为 "Selector"的函数参数将使该函数对 Objective-C 不可用

objective-c - 如何在宏中使用 block

objective-c - 当方法可以返回子类时,返回类型应该是什么?

iphone - 在类之间访问 NSMutableArray 的内容

arrays - 如何将元组的快速数组转换为 NSMutableArray?

ios - 拒绝视频通话后弹出视频通话?

cocoa - NSView 未收到 mouseUp : event when mouse button is released outside of view

cocoa - 基于 View 的 NSOutlineView 标题单元格字体问题

ios - NSMutableArray 中的 NSString 与 myString 中的不一样

iphone - 如何重新创建 iPhone 的联系人列表 UITableView