0

我已经编写了一个特定的初始化覆盖函数,我想在其中传递要在数组中调用的索引号。索引号本身是通过用户在表格视图中选择表格行来定义的。所以.. 选择的行号应传递到 init 函数并在那里用于进一步处理。

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

也许仅供参考:我在 TableViewController 中创建了一个数组副本,该副本最初位于 PortConnection 文件中。

这是必要的文件。谁能帮帮我,在哪里搜索?

便携式视图控制器.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;
}

端口连接.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

端口连接.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 有问题,但我完全不确定....

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

塞巴斯蒂安

4

1 回答 1

0

你的问题是这条线,

portArray = [NSMutableArray array];

虽然它是一个retained变量,但是当你使用属性setter方法时它会保留值。这是一个autoreleased 对象的直接分配,将在一段时间内释放并保留任何其他保留它的对象(这不会发生),它应该被释放。这是你不想要的东西。通过使用属性设置器来解决这个问题,

self.portArray = [NSMutableArray array];
于 2011-06-12T19:07:59.577 回答