0

I'm new to Objective-C, and I'm trying to find out the best way to make an "array of records" usable by all views in the project.

I already have a Singleton, and I've read that I should create a Class to act as my 'record'.

Refer to this thread as an example: How to create nested array or multidimensional array

So if I create an array of Class instances, should I just declare the array as an NSMutableArray in my existing Singleton, or is there a better/cleaner way to achieve the same goal?

Community
  • 1
  • 1
wayneh
  • 4,393
  • 9
  • 35
  • 70
  • Well if you have the singleton already, then yes you would declare an NSMutable array in it. All the classes that access that singleton would then be able to remove/add records to the array. I don't understand what you mean by "So if I create an array of Class instances" though. – Just a coder Jan 29 '12 at 16:38
  • @theface I should have said "So if I need an array..." sorry. Thanks for the answer though. Good to know I'm not totally off-base here. – wayneh Jan 29 '12 at 16:44

3 Answers3

0

You can create a data controller singleton class and use it much like Apple does in their own frameworks. Here is an example of the singleton data controller class.

// MyDataController.h

#import <Foundation/Foundation.h>

@interface MyDataController : NSObject
@property (nonatomic, retain) NSMutableArray *data;
+(MyDataController*)sharedController;
@end



// MyDataController.m    
#import "MyDataController.h"

    MyDataController *__dataController;

    @implementation MyDataController
    @synthesize data;
    -(id)init {
        self = [super init];
        if (self) {
            data = [NSMutableArray new];
        }
        return self;
    }

    +(MyDataController*)sharedController {
        if (__dataController == nil) {
            __dataController = [MyDataController new];
        }
        return __dataController;
    }

    @end

In this class the iVar data is a instance variable of the MyDataController class and __dataController is a class variable. This singleton is self initializing when you call its public static +(MyDataController*)sharedController method. You can globally access the data array like this.

[[MyDataController sharedController] data];

To make a call to this singleton you will need to #import "MyDataController.h" in every class you want to access it from or add it to your pch file to access it globally

#import <Availability.h>

#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "MyDataController.h"
#endif

By using this scheme for Objective-C singletons, you can add additional convenience methods to your class. The singleton will retain a reference to one object of its own type meaning that any additional variables can be declared as instance variables instead of declaring them as class variables.

MobileOverlord
  • 4,580
  • 3
  • 22
  • 30
  • You went over my head here - could you provide a bit more detail? 1) How would you use the new Singleton? 2) What if I also have several objects/vars in the Singleton which I do not want to be part of the 'class'? – wayneh Jan 29 '12 at 17:02
  • I updated to better illustrate how to use and call the singleton data controller. When it comes to adding your own custom objects to the array you can just use `[[[MyDataController sharedController] data] addObject:(id)object];` – MobileOverlord Jan 29 '12 at 17:25
0

i understand that you are using the approach pointed by Stephen Darlington on this link How to create nested array or multidimensional array.

So I'm assuming you are creating a records class. If you really want to write less code, then instead of creating a new class (the records class) to handle the records, why not use an NSDictionary/NSMutableDictionary?

The singleton class already has the NSMutable array. This array would be used to store the Dictionaries.

1) Saving information to the dictionaries - When ever a class (that has acces to that singleton) want to store information.. say name, age, sex. All they would do is:

NSString *personName = @"Bob";
NSString *personAge = @"25";
NSString *personSex = @"male";

NSMutableDictionary *myDict = [[NSMutableDictionary alloc] init];
[myDict setObject:personName forKey:@"name"];
[myDict setObject:personAge  forKey:@"age"];
[myDict setObject:personSex  forKey:@"sex"];

Then add this dictionary to the NSMutableArray within the singleton class to end up having an array of Dictionaries in the singleton. Dictionaries would be easy to manage the data. You can traverse the array in the singleton using a for-loop or what ever, and then when you find the Dictionary you want, you can access/compare/remove/add/what-ever the data in them. Check out the NSDictionary Reference and the NSMutableDictionary Reference to see the list of methods you can use on the Dictionary. Using a dictionary should save you coding time instead of you creating methods in your records class that do the same thing already implemented in a dictionary.

Community
  • 1
  • 1
Just a coder
  • 15,480
  • 16
  • 85
  • 138
  • I gave you credit here, but I actually used the NSMutableArray in my Singleton as mentioned in the original post and in your comment. – wayneh Feb 01 '12 at 21:03
  • Clarification, i wasn't suggesting that you *not* use the NSMutableArray in the singleton. I'm telling you to use it :). But, with your current implementation, what are you adding to that array in the singleton? you are adding instances of the *records* class right? I was suggesting that you add Dictionaries to that array instead of instances of the records class. That'll make you type less code. You wouldn't have to create a *records* class. But... your way works as well with the benefit of creating customize methods.. so it's up to you ;) – Just a coder Feb 01 '12 at 22:25
0

If you want to access the same data in multiple views you might want to look at CoreData. It is a little involved compared to your more lightweight singleton approach but you might find it saves you time in the long run. Apple provide classes to display the data in table like views and there is lots of help on the web.

brain
  • 5,496
  • 1
  • 26
  • 29