Bummer! You must be logged in to access this page.

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

Updating an Arrays content who's method uses if(myArray ==nil) to check prior to first build.

Apologises if the question isn't clear. I am working on my first app and I really wanted to do some housekeeping to tidy up my code.

I'm forming a number of arrays with sales data. I then use a number of methods which uses certain aspects of each array to perform calculations of sales targets.

When looking to tidy the code I watched the crystal ball app videos. Checking whether the array is empty before building is perfect when the arrays are used for many different things. However, as the array's are built with sales data the people using the app will want to refresh the contents of the array every few hours.

What would be the best way to update the arrays? Should I clear them when the data is refreshed and then call each method to rebuild them?

4 Answers

Hi Stephen,

you might want to consider the use of NSMutableArray instead of NSArray as IMHO this seems to make more sense for the purpose you just described (at least if I got that right :) ).

Kind Regards Holger

I am using NSMutableArrays. I said array as I didn't think the type of array made a difference. I want to refer to the array a number of times to run calculation, but I don't want to build the array every time. I will use a pull down to refresh to update the data, but how am I best to go about updating the arrays? should I 'empty' them on refreshing?

Are you using the MVC Model? Are you then persisting your Model in some way (e.g. DB, plist... ) and if so, how do you do it and why?

I'm sorry, Stephen, but at this very moment I don't fully understand what you're doing with your App or planning to do, the structure and exactly why and how you're using many NSMutableArray's. Why do you have to check if a certain NSMutableArray is empty?

Holger, sorry for the delay in responding.

I am a real beginner with app development. I have worked on my code and I now want to arrange it in the MVC model. I'm sorry, I don't know what 'persisting the model' involves.

I parse some HTML data to create the array. I want to build the array, then access it numerous times for calculations before it needs refreshing.

In the beginner iOS project on here Amit builds an array and includes the if(myArray == nil) line so that the array is rebuilt every time it is called.

I want to use a similar approach to tidy up my code. I just want to know how to 'bypass' the if(myArray == nil) when I need to refresh the array. This is why I'm thinking I need to empty it at the time of refresh.

Without knowing the details of your project I would recommend using a property for checking/conserving the status of something like:

@property (nonatomic, assign) BOOL statusArray;

...

somewhere in your code:
statusArray = YES;

...

if (statusA) {
   do something if statusArray = true;
} else {
   do something if statusArray = false;
};
Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

The approach described in the Crystal Ball app is for a static set of items. If you want to create a model object that can adapt based on the data being passed then as Holger mentioned you need to use a NSMutableArray. Let's say you have a class called SalesData. This class can contain public methods like: addData, removeData and data . In the implementation you you add and remove data to your array but you never directly provide public access to the array.

Here's an example (warning: source code has not been compiled so might contain syntax errors):

// Interface Definition
// SalesData.h

@interface SalesData : NSObject 

- (void) addData:(id)data;
- (void) removeData:(id)data;
- (NSArray*) data;
@end


// Implementation
// SalesData.m

// Class extension in the .m file to create a private property
@interface SalesData ()
@property (nonatomic, strong) NSMutableArray *items;
@end

@implementation SalesData

- (NSMutableArray*) items {
    if ( !_items ) {
         _items = [NSMutableArray array];
        // add other items here if you need to initialize it with some data.
    }
   return _items;
}

- (void) addData:(id)data {
    [self.items addObject:data];
}
- (void) removeData:(id)data {
    [self.items removeObject:data];
}

- (NSArray*) data {
    return self.items;
}


@end

Hope that answers your question.

Thanks for taking the time to answer. I'll work these methods into my app. Steve