English 中文(简体)
跨类间通过 NSM 的 NSMable 阵列
原标题:Passing an NSMutable Array Between Classes

我有一个问题,希望有人能回答。我是目标C的新人,虽然大多数都给我带来了小麻烦,但很明显,我无法正确理解在班级之间通过NSMMetableArray。

这就是我想赶上的 NSM 移动阵列的班级

#import "CanBee.h"
#import "Rat.h"
@implementation CanBee
-(id) init
{
    if ((self = [super init])) {
        if([[self parent] respondsToSelector:@selector(getRatsArray)] == YES)
        {
            ratsArray = (NSMutableArray *) [[self parent] performSelector:@selector(getRatsArray)];
        }
        [self scheduleUpdate];
    }
    return self;
}


-(void) update:(ccTime)delta
{
    for (int i = 0; i <= ratsArray.count; i++) {
        NSLog(@"in rats array loop in canBee: %i",i);
        Rat * rat = [ratsArray objectAtIndex:i];
        if (abs(self.position.x - rat.position.x) < 10 && abs(self.position.y - rat.position.y) < 10) {
            [[self parent] removeChild: rat cleanup:TRUE]; //<-- this line is probably not correct, but not relevant to my question anyway, don t worry about it for this post
        }
    }
}

@end

这是来自游戏 Layer 类的Init 函数, 我游戏的主要控制器类 。

    ratsArray = [NSMutableArray alloc];

    Rat * rat = [Rat spriteWithFile:@"rat.png"];
    [rat setPosition:ccp((4 * 53) - 25,100)];
    [rat setScale:.75];
    [self addChild:rat z:6];

    [ratsArray addObject:rat]; 

这是同一控制器类中的 拾取器函数

-(NSMutableArray *) getRatsArray {
    return ratsArray;
}

鼠标阵列是控制器类的全球性变数, 也是捕鼠者“Canbee”类的变数。

我还要提一下,我真的不想通过一个阵列的副本,而是要通过一个指针或参考,这样我的抓取类就可以与原始阵列及其包含的对象互动。

非常感谢你的帮助!

问题回答

我只想加上一个将大鼠阵列作为一个参数的原体的替代实施, 并确保给方法一个有意义的名称 。 这样您就不需要代码线从控制器中获取它。 它看起来是这样的 :

#import "CanBee.h"
#import "Rat.h"
@implementation CanBee
-(id) init
{
    if ((self = [super init])) {
    }
    return self;
}

-(id) initWithRatsArray:(NSMutableArray *)ratsArray 
{
    if ((self = [super init])) {
        ratsArray = ratsArray;
        //don t forget about proper memory management here
        [ratsArray retain]
        [self scheduleUpdate];
    }
    return self;
}

然后,当您在Canbee类中进行即时计时, 传递鼠标阵列作为参数, 比如 :

CanBee *aCanBee = [[CanBee alloc] initWithRatsArray:ratsArray];




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签