2010-10-16 66 views
1

將值設置爲我的代碼的NSArray屬性時,會出現某種錯誤。我會向你發送代碼和調試器輸出。目標C:屬性錯誤


@interface RootViewController : UITableViewController { 
@private 
NSArray* regioesArray; 
// NSString *str; 
// NSArray *ar; 
} 

@property(nonatomic, retain) NSArray *regioesArray; 

@implementation RootViewController 

@synthesize regioesArray; 
//@synthesize str; 
//@synthesize ar; 

- (void)viewDidLoad { 
[super viewDidLoad]; 

//Set Title 
self.title = @"Regiões"; 
self.regioesArray = nil; 
} 

- (void)viewDidUnload { 
    self.regioesArray = nil; 
} 


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 2; 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
if(section == 0 && self.regioesArray != nil){ 
    return [regioesArray count]; 
}else { 
    return 0; 
} 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *MyIdentifier = @"MyIdentifier"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease]; 
} 

// Set up the cell 
Zona *zona = (Zona *)[regioesArray objectAtIndex:indexPath.row]; 
cell.text = [zona nome]; 
[cell.contentView addSubview: [self getDetailDiscolosureIndicatorForIndexPath: indexPath]]; 
[zona release]; 

return cell; 
} 


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    // Navigation logic 
} 


/* 
Override if you support editing the list 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 

if (editingStyle == UITableViewCellEditingStyleDelete) { 
    // Delete the row from the data source 
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES]; 
} 
if (editingStyle == UITableViewCellEditingStyleInsert) { 
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
} 
} 
*/ 


/* 
Override if you support conditional editing of the list 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { 
// Return NO if you do not want the specified item to be editable. 
return YES; 
} 
*/ 


/* 
Override if you support rearranging the list 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { 
} 
*/ 


/* 
Override if you support conditional rearranging of the list 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { 
// Return NO if you do not want the item to be re-orderable. 
return YES; 
} 
*/ 


- (void)viewWillAppear:(BOOL)animated { 
[super viewWillAppear:animated]; 
} 

- (void)viewDidAppear:(BOOL)animated { 
[super viewDidAppear:animated]; 
} 

- (void)viewWillDisappear:(BOOL)animated { 
} 

- (void)viewDidDisappear:(BOOL)animated { 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
// Return YES for supported orientations 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 


- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview 
// Release anything that's not essential, such as cached data 
} 


- (void)dealloc { 
[super dealloc]; 
[regioesArray release]; 
} 


- (UIButton *) getDetailDiscolosureIndicatorForIndexPath: (NSIndexPath *) indexPath 
{ 
    UIButton *button = [UIButton buttonWithType: UIButtonTypeDetailDisclosure]; 
    button.frame = CGRectMake(320.0 - 44.0, 0.0, 44.0, 44.0); 
    [button addTarget:self action:@selector(detailDiscolosureIndicatorSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    return button; 
} 

- (void) detailDiscolosureIndicatorSelected: (UIButton *) sender 
{ 
    // 
    // Obtain a reference to the selected cell 
    // 
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath: [self.tableView indexPathForSelectedRow]]; 

    // 
    // Do something like render a detailed view 
    // 
    /// ... 
} 

@end 

- (void)applicationDidFinishLaunching:(UIApplication *)application { 

// Create the navigation and view controllers 
RootViewController *rootViewController = [[[RootViewController alloc] initWithStyle:UITableViewStylePlain] autorelease]; 

UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController]; 
self.navigationController = aNavigationController; 

NSArray * regioes = [self createRegions]; 
    rootViewController.regioesArray = regioes; 


[regioes release]; 
[rootViewController release]; 
[aNavigationController release]; 
// Configure and show the window 
[window addSubview:[navigationController view]]; 
[window makeKeyAndVisible]; 

} 

該錯誤是就行rootViewController.regioesArray = regioes; 我無法爲此屬性提供任何值,甚至不能「無」。

這裏是調試:

 2010-10-16 01:35:43.965 TransitoRio[4224:207] *** -[RootViewController setRegioesArray:]: unrecognized selector sent to instance 0x1813ef0 2010-10-16 01:35:43.968 TransitoRio[4224:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[RootViewController setRegioesArray:]: unrecognized selector sent to instance 0x1813ef0' 

是否有人在這裏知道什麼可能會發生?

至於問,這裏是「createRegions」的方法:

- (NSArray *)createRegions { NSMutableArray *regioes = [[NSMutableArray alloc] init]; Zona *regiao = [[Zona alloc] init]; regiao.nome = @"Zona Sul"; [regioes addObject:regiao]; [regiao release]; regiao = [[Zona alloc] init]; regiao.nome = @"Zona Norte"; [regioes addObject:regiao]; [regiao release]; regiao = [[Zona alloc] init]; regiao.nome = @"Zona Oeste"; [regioes addObject:regiao]; [regiao release]; regiao = [[Zona alloc] init]; regiao.nome = @"Centro"; [regioes addObject:regiao]; [regiao release]; // Sort the regions. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"nome" ascending:YES]; NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1]; NSArray *sortedRegions = [regioes sortedArrayUsingDescriptors:sortDescriptors]; [sortDescriptor release]; [sortDescriptors release]; [regioes release]; [sortedRegions retain]; return sortedRegions; }
+0

您是否嘗試過「全部清理」並重新編譯?有時xcode的構建內容會被搞砸,並且無法重建文件。它可能使用舊的目標代碼。無論如何,在做其他事之前,只需要嘗試一下。 – Nimrod 2010-10-16 04:41:01

+0

是啊,我試過了......但沒有交易...... – 2010-10-16 05:06:41

+0

你可以發佈你的createResions方法嗎? – 2010-10-16 05:12:39

回答

1

我看到你宣佈你的陣列是在頭文件私有,所以你不應該從類的外部訪問屬性。

+0

的確,實例變量是私有的,但是setter和getter方法仍然應該是公開的(我認爲這是實現中的綜合方法)。我可能是錯的,但我不認爲這是問題。 – 2010-10-16 22:22:46

+0

是的,我也認爲它與@private無關...... – 2010-10-19 04:24:37

+0

對,我試過這個例子,並且創建了setter/getter方法。我沒有看到問題,但需要注意的一點是:您不應該在createRegions方法中保留sortedRegions變量,因爲它已經被自動釋放了。 – brutella 2010-10-19 08:04:41