Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

How to call and use filled array from different class (IOS)?

Hi guys, I'm not sure why I'm not understanding/using this properly. I'm trying to use an array from a different class after it has been filled with objects. However, every time I use the array in the separate class, it's empty and has no objects in it. Let me know if you want me to give some examples, but I was looking to see if anyone had a working example that I could see what I am doing wrong. Thanks, Allan

5 Answers

What exactly are you trying to do? I don't think you can directly access a property of another class. Create a method in the first class that returns the array. Call that method from the second class while assigning it to a local array. You then should be able to use it like a normal array. I think this will work, my brain is half asleep though so don't hold me to that.

FirstClass.h
-----------------
+(NSArray *)returnFirstClassArray{
 return self.arrayToBeReturned;
}
SecondClass.h
------------------------------
NSArray *myArray = [FirstClass returnFirstClassArray];

I think this example should explain your question.

//  ArrayViewController.h
#import <UIKit/UIKit.h>
@interface ArrayViewController : UITableViewController
@property (nonatomic, strong) NSArray *someArray;
@end

==========================================
//  ArrayViewController.m
#import "ArrayViewController.h"
@implementation ArrayViewController
- (NSArray *) someArray {
    if (_someArray == nil) {
        _someArray = [[NSArray alloc] initWithObjects: @"Zero",
                        @"One",
                        @"Two",
                        @"Ten", nil];
    }
    return _someArray;
}
@end
===========================================
//  ViewController.h
#import <UIKit/UIKit.h>
@class ArrayViewController;
@interface ViewController : UIViewController
@property (nonatomic, strong) ArrayViewController *arrayFromOtherClass;
@end
===========================================
//  ViewController.m
#import "ViewController.h"
#import "ArrayViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *newArrayFromArray;
    self.arrayFromOtherClass = [[ArrayViewController alloc] init];
    newArrayFromArray = self.arrayFromOtherClass.someArray;
    NSLog(@" %@", newArrayFromArray[2]);
}
@end

Thanks, I'll try these out tomorrow. I was just getting frustrated with something that I knew was very basic.

So I kinda went in a different directions and used an app delegate to use a singleton instance to achieve what I wanted, (http://stackoverflow.com/questions/8931742/correct-way-to-create-use-a-singleton-nsmutablearray-for-xcode-4). So it's not the best solution but it will do for now. For your examples does this only apply to NSArray or does it work for NSMutableArray as well?

Yep, i just check that, and it's work fine.