Objective-C,类抽象和访问这些变量
我在 java 和 c++ 开发方面拥有丰富的经验,因此类和抽象数据对我来说非常容易。我不久前才开始使用 Objective C,并且主要在课堂上使用全局变量,一切进展顺利。我只是决定抽象我的大部分代码,以减少意大利面条式代码,但我遇到了一个自周六以来一直无法克服的问题。
大纲:
我正在制作一个纸牌游戏。我最近将卡片数据抽象为一个包含 3 个变量的类; (int)i_cardValue、(int)i_cardSuit 和 (UIImage *)uii_cardImage。我通过调用创建了一张卡,
-(PlayingCard *)initWithValue:(int)i_initValue suit:(int)i_initSuit image:(UIIMage*)uii_initImage {
[self setCardValue:i_initValue];
[self setCardSuit:i_initSuit];
[self setCardImage:i_initImage];
return self;
}
我还抽象了引擎部分(基本上是牌组功能,例如洗牌和牌组管理)。甲板是一个可变数组,我有一个初始化甲板函数。
NSMutableArray *nsma_deck;
-(void)initDeck {
nsma_deck = [[NSMutableArray alloc] initWithCapacity:52];
}
现在,在我的视图控制器中,我调用 initDeck,然后使用诸如
[pce_Engine initDeck];
[[pce_Engine nsma_deck] addObject:[[PlayingCard alloc] initWithValue:13 suit:2 image:[UIImage imageNamed:@"2C.png"]]];
pce_Engine 是 PlayingCardEngine 类型的变量之类的行将项目添加到牌组中。我将 NSLog 行放入 initDeck 中,当我运行程序时它们不会显示在控制台中。它们是否没有被调用,因此数组没有被分配,因此我无法向数组添加内容?我以为我做得很好,但要么我做错了,要么我错过了一些东西,因为将它添加到视图中不会让它显示。使用 NSLog,牌组 == nil,所以我想问题是我没有正确访问牌组,因此没有添加值并且到处只有空变量:(。我尝试查看很多目标 c 的教程,但我什至没有找到一个抽象的,所以我无法找到我的问题,
有人能指出我正确的方向吗?
I have a lot of experience with java and c++ development, so classes and abstracting data is pretty easy for me. I only started objective C a short time ago, and i was mostly working with in class globals, and everything was progressing smoothly. I just decided to abstract a large portion of my code in an effort to make it less spaghetti code, and i hit a problem that i haven't been able to overcome since Saturday.
Outline:
I am making a playing card game. I recently abstracted the card data into a class containing 3 variables; (int)i_cardValue, (int)i_cardSuit, and (UIImage *)uii_cardImage. I create a card by calling
-(PlayingCard *)initWithValue:(int)i_initValue suit:(int)i_initSuit image:(UIIMage*)uii_initImage {
[self setCardValue:i_initValue];
[self setCardSuit:i_initSuit];
[self setCardImage:i_initImage];
return self;
}
I also abstracted the engine part (basically deck functions, such as shuffle and deck managment). The deck is a mutable array, and i have an init deck function.
NSMutableArray *nsma_deck;
-(void)initDeck {
nsma_deck = [[NSMutableArray alloc] initWithCapacity:52];
}
Now, in my view controller, i make a call to initDeck, then i add items to the deck with a line such as
[pce_Engine initDeck];
[[pce_Engine nsma_deck] addObject:[[PlayingCard alloc] initWithValue:13 suit:2 image:[UIImage imageNamed:@"2C.png"]]];
pce_Engine is a variable of type PlayingCardEngine. I put NSLog lines into the initDeck, and they do not display in console when i run my program. Are they not being called, so the array isnt getting alloced, and thus i cant add stuff to the array? I thought i did this all right, but either im doing it wrong or im missing something, because adding it to the view does not get it to display. Using an NSLog, the deck == nil, so i guess the problem is that i am not accessing the deck correctly and thus not adding values and only have empty variables everywhere :( . I tried looking at a lot of tutorials for objective c, but I haven't even found one that abstracts as far as I am going, so I haven't been able to find my problem.
Can anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您的卡类是
NSObject
的子类,在这种情况下,您应该实现指定的初始化程序(init
),它调用[super init]
并执行通用设置。在你的 initWith... 方法中,你应该调用[self init]
然后进行赋值。这是一篇关于 cocoa/objective-c 初始化器的好文章: http://mikeash.com/?page=pyblog/the-how-and-why-of-cocoa-initializers.html
甲板管理类也是如此。希望这有助于找到您的问题。
I presume your card class is a subclass of
NSObject
in which case you should implement designated initializer (init
) which calls[super init]
and does generic setup. And in your initWith... method you should call[self init]
and then do the assignment.Here's a good post about cocoa/objective-c initializers: http://mikeash.com/?page=pyblog/the-how-and-why-of-cocoa-initializers.html
Same goes with class for deck management. Hope this helps to find your problem.
首先,我同意 Eimantas 的观点,即您的 init 方法缺少对 [super init] 的调用(您应该返回其值,请阅读正常的 init 方法是什么样子)。
但至于 init_deck - 你的视图控制器中的什么方法有这两行?我认为它的调用并不是更多地基于调用代码的位置(尽管当调用该代码时 pce_Engine 也可能为零,在这种情况下它根本不起作用)。
First of all, I agree with Eimantas that your init method is missing a call to [super init] (the value of which you should be returning, read up on what a normal init method looks like).
But as for the init_deck - just what method in your view controller do you have those two lines? I'm thinking it's not being called more based on where the calling code is than anything else (although pce_Engine may also be nil when that code is called in which case it would not work at all).