我有一個DetailViewController和一個消息類(這是一個TableViewController類)。我解析了第一類中的一些Web信息,並希望在第二課中使用它們中的一些值。當我在這裏尋找周圍和谷歌幾個haurs,現在我想我需要在我的第二類中定義一些extern
變量,並初始化它們與第一類的對象..我嘗試了幾種方法,但都失敗了。是否可以使用extern訪問Objective c中其他類的變量?
在我的第一CLAS我有一個NSMutableArray變量稱爲messID,在第二類我這樣做:
#import DetailViewController
.
.
extern NSMutableArray *myArray;
DetailViewController *myObject;
myArray=myObject.messID;
但我得到了以下錯誤:
Undefined symbols for architecture i386:
"_myArray", referenced from:
-[messages tableView:cellForRowAtIndexPath:] in messages.o
我在做什麼錯我可以做什麼請任何一個幫助嗎?..
編輯 我輸入了需要的類, 在第二類的.h文件我用這個:
@interface messages : UITableViewController{
BNT_1DetailViewController *myObject;
}
@property(retain, nonatomic)BNT_1DetailViewController *myObject;
而且其.m文件是:
...
@sysnthesize myObject;
viewDidLoad{
myObject=[[BNT_1DetailViewController alloc]init];}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return myObject.mesID.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//extern NSMutableArray *messID;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}NSLog(@" -> %@",myObject.mesID);
cell.textLabel.text= [myObject.mesID objectAtIndex:indexPath.row];
// Configure the cell...
// [tableView reloadData];
return cell;
}
你的意思是不需要extern命名空間? –
請參閱我對「extern」關鍵字的編輯。你不需要它,如果你想讓你的DetailController成爲一個全局對象,你可以使用它,但我懷疑它。你的代碼在任何情況下都缺少實際定義'extern'關鍵字引用的對象的部分。 – sergio
在第一個類中,我用來自JSON對象的一些值初始化數組,並且我想在第二個類中使用它們。我試過這個,但結果與之前一樣.. –