views:

112

answers:

6

First off, I despise singletons with a passion. Though I should probably be trying to use one, I just don't want to. I want to create a data class (that is instantiated only once by a view controller on loading), and then using a different class, message the crap out of that data instance until it is brimming with so much data, it smiles.

So, how do I do that? I made a pointer to the instance of the data class when I instantiated it. I'm now over in a separate view controller, action occurs, and I want to update the initial data object. I think I need to reference that object by way of pointer, but I have no idea how to do that. Yes, I've set properties and getters and setters, which seem to work, but only in the initial view controller class.

Peace Love applesauce.

A: 

Make a global variable for your object and store it there on creation. You can wire that up in the init method (probably bad style), or from the caller or via interface builder. Just make your variable known in the files that use it.

Or - well - use some kind of singleton pattern and get the instance directly from that class. Looks much cleaner.

Eiko
A: 

Seriously use a singleton. In case you don't like them cause you don't know the code:

@interface Order : NSObject {
NSMutableArray *order;
}

@property (nonatomic, retain) NSMutableArray *order;

+ (Order *)sharedInstance;

@end

#import "Order.h"


@implementation Order

@synthesize order;

+(Order *)sharedInstance {
static Order *myInstance = nil;

@synchronized(self) {
    if(!myInstance) {
        myInstance = [[Order alloc] init];
        }
    }
return myInstance;
}

-(void)dealloc {
    [order release];
    [super dealloc];

}
@end
Rudiger
What if we don't like them because they're bad? What if we don't like them because we prefer to avoid globals? What if we don't like them because they're hard to make thread-safe? What if we don't like them because they're unnecessary? What if we don't like them because they needlessly constrain our code and prevent reuse?
jalf
Sometimes you just need globals its rare but it happens, if you find it hard to make them thread safe in the iPhone SDK because it does it all for you you're doing it wrong, just like globals sometimes you just need them. You are making a sweeping statement. Just like people say you should never use tables in HTML, if you want to display a table, use a table. Don't get into the elitist stuff. Although I jumped to conclusions about what the author was doing all I was doing was posting a singleton code because when I first needed a global it was hard to code and not thread safe singlton fixed it
Rudiger
My reason was I made a tab bar app, I had one variable that was used across all tabs, it was started in the app delegate. I could use a global which was harder to access, I could write it to a file (slow on iPhone and unnecessary as next time the App was opened I didn't need it) or a database (overkill) so whats left?
Rudiger
+1  A: 

There's no need to use a singleton if you don't like the pattern or if it doesn't fit. Assuming you are creating your second view controller in the first one, just declare an ivar and property for your model object in your second view controller and when you instantiate it, assign the model object to this property.

Ole Begemann
Thats true but if if hes not creating the second view in the first one then he will run into trouble. When the author said "I'm now over in a separate view controller" I assumed the two were unrelated. I guess its hard to give an exact answer without more imformation.
Rudiger
A: 

Um. Hello. Isn't Core Data a good enough framework for you? It allows you to have a single persistent store and multiple contexts to manage updates and merging of changes in response to notifications. I may be out of line here, but seeing how you start the question with a strong opinion about a well accepted pattern in your first question indicates that you have not spent much time discovering the ways in which the objective c runtime and Foundation classes in iOS can collaborate to achieve a task. In any software, one object and only one object owns a specific resource. You should embrace singletons. I suggest you spend some time reading http://www.cocoadesignpatterns.com/. Oh yeah, check out the meaning of KVO.

falconcreek
A: 

Why not make it a property of your app delegate? That way you don't have to use the singleton pattern but you are taking advantage of Apple's already existing usage of the singleton pattern.

JeremyP
A: 

Don't forget that Objective-C is a superset of C.

Basically, a data class is a plain C struct.

If you want to access a variable of that class from another class, make it global.

mydata.h:

struct MyData {
    // Whatever data fields that you need, e.g.:
    NSInteger value;
};

extern struct MyData mydata;

mydata.c:

struct MyData mydata = {
    // Whatever initial value that you find relevant, e.g.:
    .value = 42;
};
mouviciel