2011-02-07 7 views
0

あるクラスで作成された配列を別のクラスに渡そうとしています。私はデータにアクセスできますが、countを実行すると、配列内に0個の項目があることがわかります。あるObjective-Cクラスから別のObjective-Cクラスに配列を渡す

これは、peopleArrayのデータが設定されている場所で、以下に示すコードとは異なるクラスにあります。

[self setPeopleArray: mutableFetchResults]; 

for (NSString *existingItems in peopleArray) { 
    NSLog(@"Name : %@", [existingItems valueForKey:@"Name"]); 
} 

[peopleArray retain]; 

これは私が別のクラスから配列を取得する方法ですが、それは常にcount = 0

int count = [[dataClass peopleArray] count]; 
NSLog(@"Number of items : %d", count); 

私のコードの残りの部分を出力します。

#import <UIKit/UIKit.h> 
#import "data.h" 


@class rootViewController, data; 

@interface login : UIView <UITextFieldDelegate>{ 

    rootViewController *viewController; 
    UIButton *loginButton; 
    UIButton *newUser; 
    UITextField *entry; 
    data *dataClass; 
} 


@property (nonatomic, assign) rootViewController *viewController; 
@property (nonatomic, assign) data *dataClass; 

- (id)initWithFrame:(CGRect)frame viewController:(rootViewController *)aController; 
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField; 


@end 

data.h 

#import <UIKit/UIKit.h> 
#import "People.h" 

@class rootViewController; 

@interface data : UIView <UITextFieldDelegate>{ 
    rootViewController *viewController; 
    UITextField *firstName; 
    UITextField *lastName; 
    UITextField *phone; 
    UIButton *saveButton; 
    NSMutableDictionary *savedData; 

    //Used for Core Data. 
    NSManagedObjectContext *managedObjectContext; 
    NSMutableArray *peopleArray; 
} 

@property (nonatomic, assign) rootViewController *viewController; 
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; 
@property (nonatomic, retain) NSMutableArray *peopleArray; 


- (id)initWithFrame:(CGRect)frame viewController:(rootViewController *)aController; 
- (void)setUpTextFields; 
- (void)saveAndReturn:(id)sender; 
- (void)fetchRecords; 

@end 




data.m(some of it at least) 

@implementation data 
@synthesize viewController, managedObjectContext, peopleArray; 

- (void)fetchRecords { 

    [self setupContext]; 

    // Define our table/entity to use 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"People" inManagedObjectContext:managedObjectContext]; 

    // Setup the fetch request 
    NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
    [request setEntity:entity]; 

    // Define how we will sort the records 
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:NO]; 
    NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; 


    [request setSortDescriptors:sortDescriptors]; 
    [sortDescriptor release]; 

    // Fetch the records and handle an error 
    NSError *error; 
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy]; 


    if (!mutableFetchResults) { 
     // Handle the error. 
     // This is a serious error and should advise the user to restart the application 
    } 

    // Save our fetched data to an array 
    [self setPeopleArray: mutableFetchResults]; 

    for (NSString *existingItems in peopleArray) { 
     NSLog(@"Name : %@", [existingItems valueForKey:@"Name"]); 
    } 

    [peopleArray retain]; 
    [mutableFetchResults release]; 
    [request release]; 

    //NSLog(@"this is an array: %@", eventArray); 
} 

login.h

login.m

#import "login.h" 
#import "data.h" 

@interface login (PrivateMethods) 
- (void)setUpFromTheStart; 
- (void)loadDataScreen; 
-(void)login; 
@end 

@implementation login 
@synthesize viewController, dataClass; 


-(void)login{ 

    int count = [[dataClass peopleArray] count]; 
    NSLog(@"Number of items : %d", count); 
} 
+0

を脇に置いておきます。私は括弧の中に 'retain'と宣言されたpeopleArrayプロパティをセットしました。その場合、setPeopleArrayは自動的に保持されます。ビルドと分析をチェックしてください。これらの問題を捉えるのは良いことです。 –

答えて

1

同じオブジェクトですか?もしそうなら、あなたが持っているものはうまくいくはずです。どのようにしてdataClassインスタンスを取得しているかを確認してください。新しいインスタンスを割り当てると、別のオブジェクトから配列を取得しません。

編集:以下のコメントから、クラスとオブジェクトの違いについて混乱があるようです。私は説明しようとします(私はそれを単純化するつもりです):

クラスはあなたがXcodeで書くものです。これは、実行時にオブジェクトを作成およびアクセスする方法をアプリケーションに知らせる説明です。これは、(インスタンス変数に基づいて)どのくらいのメモリを割り当てるのか、どのメッセージを送ることができるのか、そしてどのようなコードを呼び出すべきかを調べるために使用されます。クラスは、実行時にオブジェクトを作成するための青写真です。

オブジェクトは実行時にのみ存在します。 1つのクラスに対して、そのクラスの多くのオブジェクトを作成できます。それぞれには独自のメモリが割り当てられており、互いに区別されています。あるオブジェクトにプロパティを設定した場合、他のオブジェクトは変更されません。メッセージをオブジェクトに送信すると、メッセージを送信するメッセージだけが受信されます。同じクラスのすべてのオブジェクトではありません。

例外があります。たとえば、クラスプロパティ(最初に-の代わりに+)を作成すると、すべてのオブジェクト間で共有されます。メモリには1つしか作成されません。すべて同じものを指します。

また、*で宣言されたものはすべてポインタなので、すべてのポインタプロパティが同じデータを指すようにすることができます。ポインタ自体は共有されていません。

dataClassがnilであるため、[dataClass peopleArray]はnilであり、countメッセージの呼び出しも同様です。あなたはnilにメッセージを送信できますが、クラッシュすることはありませんが、何も役に立ちません。

ログインオブジェクトの作成方法がわかりません。そうであれば、そのdataClassプロパティを設定する必要があります。

デバッガでコードを実行し、ブレークポイントを設定し、変数を調べてみてください。

+0

まあ私はdata.mから配列をlogin.mに取得しようとしているので、#import "data.h"で十分ですか?または、@ class dataとdata * dataClassを使用してクラスを宣言する必要があります。今私は両方を使用していますが、これに関する知識は限られています。ありがとう –

+0

単一のクラスの多くのオブジェクトがあります。同じオブジェクトを参照するdataClassを取得する必要があります - dataClassはどのようにその価値を得るのですか? –

+0

importと@classは、クラスがデータ型のオブジェクトを使用できるようにします。それから、正しいものにアクセスするのはあなた次第です。それ以上のコードなしで、より具体的なアドバイスをするのは難しいです。 peopleArrayを作成するオブジェクトは、何らかの形でそれを使用するオブジェクトで使用できる必要があります。または、2番目のオブジェクトが参照を持つためにpeopleArrayを渡すことができます。 –

0

コードからは、可変配列を渡しているようです。

[self setPeopleArray: mutableFetchResults]; 

おそらく、配列の項目は呼び出し側のクラス/メソッドのどこかで削除されます。または、最初にmutableFetchResultsを取得したクラスによって配列がリセットされます。

+0

私は配列 "mutableFetchResults"をリリースしました。これは既に設定した後でpeopleArray関数に影響しますか? –

+0

配列がすでに保持されているため、配列を消去するmutableFetchResultsでremoveAllObjectsを呼び出すと、単純なリリースには影響しません。 setPeopleArrayに配列のコピーを作成してみてください。 – adarsha

関連する問題