2011-07-19 18 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]た、aからdestinationポイントをオブジェクトaを作成します。 aはあなたが保持します。
  2. (Destination *)[destinationsArray objectAtIndex:indexPath.row]あなたには別のオブジェクトbがありますが、それはあなたに保持されません。 destinationbを指すようになりました。誰もがaを持っていません。
  3. releaseは、destinationが指すオブジェクト、つまりbに送信されます。これは、保持解除ルールに違反しています。 bではなく、aをリリースする必要があります。だから、

、代わりに、ちょうどrelease一部せず

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

を行います。

アドバイス:プロジェクトをビルドするときには常にAnalyzeBuildメニューの下にあります)を実行してください。このアナライザーは、あなたを含む一般的なタイプのエラーを検出するように設計されています。すべてのアナライザーの警告が消えるようにコードを修正してください。アナライザーの警告は、常にお客様のエラーとみなすべきです。

+1

"オブジェクトへのポインタであり、実際のポインタではありません。"私はあなたが本当の目的を意味したと思う。 – albertamg

+0

あなたはそうです:p – Yuji

+0

ありがとう!目標の鋼鉄!!!解説に加えて、ソリューションに感謝します。 –

関連する問題