I am making a very simple game, but I cannot decide on how to store the variables my custom classes will use. I only know of two ways to do this, but other people seem to have rules about where you put certain things. I would like to know how I should store the variables I am using efficiently, and I was hoping I could get the answer here.
Basically, this is my header file.
#import <Foundation/Foundation.h>
#import "URWInventoryView.h"
@interface RWInventory : NSObject {
URWInventoryView *inventoryWindow;
BOOL slotIsOccupied[5][5];
int numberOfSlots;
int numberOfSlotsFilled;
int numberOfColumns;
int numberOfRows;
}
//
////
//// Initializers
////
//
- (id) initWithSlots:(int)slots;
- (id) initWithColumns:(int)columns andRows:(int)rows;
- (id) initWithInventoryData:(RWInventory*)inventory;
//
////
//// Setters
////
//
- (void) setSlots:(int)slots;
- (void) setColumns:(int)columns;
- (void) setRows:(int)rows;
- (void) setSlotIsOccupiedColumn:(int)column andRow:(int) row;
- (void) moveItemAtColumn:(int)column andRow:(int)row toColumn:(int)newColumn andRow:(int)newRow;
//
////
//// Getters
////
//
- (int) slots;
- (int) columns;
- (int) rows;
- (BOOL) slotIsOccupiedColumn:(int)column andRow:(int)row;
@end
That uses the first method I was kinda talking about earlier. The second method involves using a single NSDictionary between the #import‘s and the @interface declaration, and they simply store each object’s data in a key. The method used above sounds much easier and efficient to me.
Please keep in mind that I will be using anywhere between five and twenty objects of this type. My last note: URWInventoryView is another one of my custom classes, and it is based on a UIView.
It’s not completely clear but I’ll take a shot at it. Forgive me if I’m not in the right ballpark here. From what I was able to parse from the question, your question basically boils down to two options:
Unless there’s a reason why you need global access I would recommend using instance data per view. I would always start with data being localized to the data that needs it and then fall back to making global data if required. If data did need to be shared between views I would create a model and consider a singleton pattern to get access to the model (InventoryModel *inv = [InventoryModel sharedInstance]). Search for singleton objective-c.
But once again, unless there’s a need, I would localize the data to the view that operates on that data. Global and shared data can always complicate lifetime of the data and possibly synchronization.