我寫了一個特定的init-override函數,我想傳遞一個索引號在數組中調用。索引號本身是通過在用戶的表格視圖中選擇一個表格來定義的。所以..所選擇的rownumber應該傳入init函數並在那裏用於進一步處理。通過函數調用傳遞的索引訪問數組
那麼..現在有我的問題..在我看來,我創建的方法都是正確的編碼。但是,當我單擊我定義的連接按鈕時,控制檯中會顯示一條錯誤消息,該索引超出範圍。所以..我已經檢查了陣列的條目,並有全部可用。所以indexnumber應該沒問題。
也許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 但我不確定....
非常感謝您給我建議!
塞巴斯蒂安
哇..不是它的工作...感謝..但是我還是不明白,爲什麼這是個問題......爲什麼是self.portArray作用不同於只有portarray? self.portArray不是直接分配? – konturgestaltung 2011-06-12 19:26:45
不,做'self.portArray ='相當於調用'setPortArray:'方法。該方法根據您如何定義屬性類型自動生成。它將釋放先前的值,然後保留,複製或分配(基於'@ property' define)新值到'portArray'。所以兩者都不一樣。 – 2011-06-12 19:33:20
如果我想設置一個自動釋放對象,那麼總是需要這樣做嗎?我的意思是,我是否總是必須像這樣編寫代碼,並且僅在例如聲明我創建的對象並且不是自動釋放對象時才使用portArray? – konturgestaltung 2011-06-12 19:49:18