あるクラスで作成された配列を別のクラスに渡そうとしています。私はデータにアクセスできますが、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);
}
を脇に置いておきます。私は括弧の中に 'retain'と宣言されたpeopleArrayプロパティをセットしました。その場合、setPeopleArrayは自動的に保持されます。ビルドと分析をチェックしてください。これらの問題を捉えるのは良いことです。 –