簡體   English   中英

Objective-C:將值設置為 NSArray 屬性時的屬性錯誤

[英]Objective-C: Properties error when setting a value to a NSArray property

為我的代碼的 NSArray 屬性設置值時出現某種錯誤。 我將向您發送 de 代碼和調試器輸出。

@interface RootViewController : UITableViewController {
 @private
 NSArray* regioesArray;
}

@property(nonatomic, retain) NSArray *regioesArray;
@implementation RootViewController

@synthesize regioesArray;

- (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
}

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

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

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

- (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; 我不能為這個屬性提供任何值,甚至不能為“nil”。

這是調試:

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;
}

我看到您在頭文件中將數組聲明為私有,因此您不應從類外部訪問該屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM