James Dunay
James Dunay

Reputation: 2724

Pass array from one Objective-C class to another

Im attempting to pass an array that is created in one class into another class. I can access the data but when I run count on it, it just tells me that I have 0 items inside the array.

This is where peopleArray's data is set up, it's in a different class than the code that is provided below.

[self setPeopleArray: mutableFetchResults];

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

[peopleArray retain];

This is how I get the array from another class, but it always prints count = 0

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

The rest of my code:

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

#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

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

Upvotes: 0

Views: 6666

Answers (2)

Lou Franco
Lou Franco

Reputation: 89172

Is it the same object? If so, what you have should work. Check to see how you are getting the dataClass instance -- if you alloc a new one, you don't get the array from the other object.

Edit: From your comments below, it appears that you are having some confusion on the difference between classes and objects. I will try to explain (I'm going to simplify it):

A class is what you write in Xcode. It's the description that lets your application know how to create and access objects at run-time. It is used to figure out how much memory to allocate (based on instance variables) and what messages can be sent, and what code to call when they are. Classes are the blueprints for creating objects at runtime.

An object only exists at run-time. For a single class, many objects of that class can be created. Each is assigned its own memory and they are distinct from each other. If you set a property in one object, other objects don't change. When you send a message to an object, only the one you send it to receives it -- not all objects of the same class.

There are exceptions to this -- for example if you create class properties (with a + instead of a - at the beginning), then they are shared between all objects -- there is only one created in memory, and they all refer to the same one.

Also, since everything declared with a * is a pointer -- you could arrange for all pointer properties to point to the same data. The pointer itself is not shared.

Edit (based on more code): dataClass is nil, [dataClass peopleArray] is therefore nil, and then so is the count message call. You can send messages to nil, and not crash, but you don't get anything useful.

I don't see how the login object is created. When it is, you need to set its dataClass property.

Try running the code in the debugger, setting breakpoints, and looking at variables.

Upvotes: 1

adarsha
adarsha

Reputation: 45

From the code, it looks like you are passing a mutable array.

[self setPeopleArray: mutableFetchResults];

Probably the items of the array are removed somewhere in your calling class / method. Or the array is reset by the class from which you get the mutableFetchResults in the first place.

Upvotes: 0

Related Questions