0

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);
}
1
  • as an aside, be careful with your retaining. I the peopleArray property declared with retain in the parentheses, and you synthesized the set? If so, setPeopleArray automatically retains. Check Build and Analyze -- it's good at catching these problems. Commented Feb 7, 2011 at 0:22

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Well I'm trying to get the array from data.m into login.m, so would using #import "data.h" suffice? Or would i need to declare the class using @class data and then data *dataClass. Right now i am using both, but my knowledge on this is limited. Thanks
There can be many objects of a single class. You need to get dataClass to refer to the same object -- how does dataClass get its value?
The import and @class just let your class use any object of type data. Then it's up to you to access the right one. It's hard to give more specific advice without more code. The object that creates the peopleArray must be available to the one that uses it somehow. Or it could pass in the peopleArray for the second object to have a reference to.
Do you think you could give me a quick example? I have the array established set up in the data.h file and then populate it inside the corresponding array. In the login.h file are you telling me that i should not use "data *dataView" and then in the .m file use [dataClass peopleArray] to target the array? Because this does not return me an error but simply the array turns up empty...
you can use data *dataView (you have to), but what do you assign to dataView and dataClass? It's pretty hard to provide an example. You should paste in more of your code, and then will be able to give better advice.
|
0

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.

2 Comments

Well i release the the array "mutableFetchResults," does that effect the peopleArray function after I have already set it?
Simple release should not affect since the array is already retained but if you call removeAllObjects on mutableFetchResults that will clear the array. Try creating a copy of the array in setPeopleArray.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.