2011-07-19 71 views
4

另一個新手問題,就在我以爲我開始在ios編程中獲得一個很小的句柄。啊!我正在關注來自 appcodeblog.com的tutoria,我正在構建一個簡單的標籤欄應用程序,利用 核心數據輸入,顯示和搜索度假目的地。我已經在教程中工作過 ,並且有一個工作應用程序,但我注意到當我選擇 「顯示目的地」選項卡時,出現以下錯誤。該應用似乎繼續 工作,但錯誤記錄到控制檯。我試圖調試 的問題,並確切瞭解發生了什麼,但我只是不明白 明白什麼是錯的。我「認爲」我有我的 ShowDestinations.xib文件的問題,我錯誤地將我的對象連接到 xib中。任何幫助深表感謝。在此先感謝您的幫助和 時間。無法在NSManagedObject類上調用指定的初始值設定項

這裏的錯誤,「CoreDataTabBarTutorial [1262:207]。未能呼籲NSManagedObject類‘目的地’指定 初始化

我不知道通過展示提供什麼樣的代碼,所以我已經開始了我頭 和實現文件ShowDistinationsViewController.h和 ShowDestinationsViewController.m

ShowDistinationsViewController.h

#import <UIKit/UIKit.h> 


@interface SearchDestinationsViewController : UIViewController { 

    UISearchBar *destinationSearchBar; 
    UITableView *searchTableView; 

    NSFetchedResultsController *fetchedResultsController; 
    NSManagedObjectContext *managedObjectContext; 

    NSArray *fetchedObjects; 

} 

@property (nonatomic, retain) IBOutlet UISearchBar *destinationSearchBar; 
@property (nonatomic, retain) IBOutlet UITableView *searchTableView; 

@property (nonatomic, retain) IBOutlet NSFetchedResultsController *fetchedResultsController; 
@property (nonatomic, retain) IBOutlet NSManagedObjectContext *managedObjectContext; 

@end 

ShowDestinationsViewController.m

#import "ShowDestinationsViewController.h" 
#import "Destination.h" 

@implementation ShowDestinationsViewController 

@synthesize destinationsTableView; 
@synthesize destinationsArray; 
@synthesize fetchedResultsController; 
@synthesize managedObjectContext; 

// Not sure where the following code came from so I commented it out!!! It didn't seem to break anything when I commented it out 
//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
//{ 
// self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
// if (self) { 
//  // Custom initialization 
// } 
// return self; 
//} 

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

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

/* 
// Implement loadView to create a view hierarchy programmatically, without using a nib. 
- (void)loadView 
{ 
} 
*/ 

/* 
// Implement viewDidLoad to do additional setup after loading the view, typically from a  nib. 
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
} 
*/ 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

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


#pragma mark - 
#pragma Data Fetch from Core Data 

- (void) viewWillAppear:(BOOL)animated 
{ 

    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Destination" inManagedObjectContext:managedObjectContext]; 
    [request setEntity:entity]; 
    NSError *error = nil; 
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 
    if (mutableFetchResults == nil) 
    { 
     // Handle the error. 
     NSLog(@"mutableFetchResults == nil"); 
    } 
    [self setDestinationsArray:mutableFetchResults]; 
    [request release]; 
    [destinationsTableView reloadData]; 
} 


#pragma mark - 
#pragma mark Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [destinationsArray count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    // Configure the cell... 
    Destination *destination = [[Destination alloc] init]; 
    destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = destination.name; 
    [destination release]; 

    return cell; 
} 

#pragma mark - 
#pragma mark Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

} 

@end 
+0

請發送'Destination.h,.m',這似乎是更多的問題的來源。 – Yuji

+0

我想我找到了問題;你不必發佈它。 – Yuji

+0

@ 5lb Bass [你不能實例化一個沒有關聯NSManagedObjectContext的NSManagedObject子類](http://stackoverflow.com/questions/1556304/cocoa-touch-nsmanagedobject-exception-when-setting-a-property/1556370#1556370 ) – albertamg

回答

6

問題似乎在於

Destination *destination = [[Destination alloc] init]; 
destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row]; 
[destination release]; 

第一行是不必要的:在Objective-C,Destination*是一個指向該對象,而不是所述真實對象。你想要的Destination對象大概已經在數組中。所以你不必創建一個指向的對象,在[[Destination alloc] init]行中,該行在下一行立即消失。這是怎麼回事是

  1. [[Destination alloc] init]創建一個對象adestinationaa由您保留。
  2. (Destination *)[destinationsArray objectAtIndex:indexPath.row]給你另一個對象b,它不會被你保留。 destination現在指向b。沒有人再持有a
  3. release被髮送到由destination指向的對象,即b。這違反了扣留規則;你應該發佈a,而不是b

所以,相反,只是做

Destination *destination = (Destination *)[destinationsArray objectAtIndex:indexPath.row]; 

沒有release部分。

建議:始終運行Analyze(在Build菜單下可用),當您構建項目時。該分析儀設計用於捕捉常見類型的錯誤,包括您的錯誤。糾正你的代碼,以便所有的分析儀警告消失;您應始終將分析儀警告視爲您的錯誤。

+1

「指向對象的指針,而不是真正的指針。」我認爲你的意思是真實的客體。 – albertamg

+0

你是對的:p – Yuji

+0

謝謝Yuji!目標鋼!感謝解釋以及解決方案。 –

相關問題