通过函数调用传递的索引访问数组

时间:2011-06-12 13:53:25

标签: arrays cocoa function variables objective-c-2.0

我编写了一个特定的init-override-function,我希望传递一个索引号,以便在数组中调用。索引号本身是通过用户在tableview中选择tablerow来定义的。所以..选择的rownumber应该被传递到init函数并在那里用于进一步处理。

嗯..现在有我的问题..在我看来,我创建的方法都是正确编码的。但是,当我单击我定义的Connect按钮时,控制台中会出现一条错误消息,即索引超出范围。所以..我已经检查了数组的条目,并且都有可用的。所以索引号应该没问题。

也许fyi:我在TableViewController中创建了一个最初位于PortConnection文件中的数组副本。

以下是必要的文件。任何人都可以帮助我,在哪里搜索?

PORTTABLEVIEWCONTROLLER.M

- (IBAction)pushConnect:(id)sender {
    NSInteger selectedRow = [tableView selectedRow];
    [portConnection initPort:selectedRow];
}

- (id)init {
    self = [super init];
    if (self) {
        // Initialization of port Connection instance
        portConnection = [[PortConnection alloc] init];
        // Fill array in Portconnection.m with devices
        [portConnection listDevices];
        // Fill tableView Data Array with data from portConnection array
        self.tableViewDataArray = [NSMutableArray arrayWithArray:portConnection.portArray];
    }
    return self;
}

PORTCONNECTION.H

@

interface PortConnection : NSObject {
    // Instance of AMSerialPort
    AMSerialPort *port;

    // Port Array to be filled with found ports
    NSMutableArray *portArray;
}

// List Devices into an given array
- (void)listDevices;

// Connect to selected port
- (void)initPort:(NSInteger)selectedRow;

@property (nonatomic, retain) NSMutableArray *portArray;
@property (nonatomic, retain) AMSerialPort *port;
@end

PORTCONNECTION.M

@implementation PortConnection

@synthesize port;
@synthesize portArray;

#pragma mark -
#pragma mark Serial Port Access

- (void)listDevices {

    // get an port enumerator
    NSEnumerator *enumerator = [AMSerialPortList portEnumerator];
    AMSerialPort *aPort;

    while ((aPort = [enumerator nextObject])) {
        [portArray addObject:[PortItem portItemWithTitle:[aPort name] andPath:[aPort bsdPath]]];
    }
}

- (void)initPort:(NSInteger)selectedRow {
    //Create object of selected port searched in array
    PortItem *portSelected = [portArray objectAtIndex:selectedRow];
    NSString *deviceNameSelected = [portSelected valueForKey:@"bsdPath"];

    // Start Connection
    if (![deviceNameSelected isEqualToString:[self.port bsdPath]]) {
        [self.port close];
        [self setPort:[[[AMSerialPort alloc] init:deviceNameSelected withName:deviceNameSelected type:(NSString *)CFSTR(kIOSerialBSDModemType)] autorelease]];
        [self.port setDelegate:self.port];

        if ([self.port open]) {
            NSLog(@"Connected...");
            [self.port setSpeed:B38400];
            [self.port readDataInBackground];
        } else {
            NSLog(@"error connecting");
            [self setPort:nil];
        }
    }
}

#pragma mark -
#pragma mark initialization / deallocation

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

- (void)dealloc {
    portArray = NULL;
    [super dealloc];
}

@end

嗯..我的想法是,INITPORT方法有问题:(NSINTEGER)SELECTEDROW 但我完全不确定......

非常感谢你再次给我建议!

塞巴斯蒂安

1 个答案:

答案 0 :(得分:0)

你的问题就在这一行,

portArray = [NSMutableArray array];

虽然它是retain ed变量,但在使用属性setter方法时它将保留该值。这是autorelease d对象的直接赋值将在一段时间内被释放并且禁止任何其他保留它的对象(这不会发生),它应该被释放。这是你不想要的东西。通过使用属性设置器

来解决这个问题
self.portArray = [NSMutableArray array];
相关问题