Reputation: 43
Im programming with objective c for ios. I have a problem...i create a class object this is the Item.h
@interface Item : NSObject {
NSString *date;
NSMutableArray *a;
NSMutableArray *b;
NSMutableArray *c;
NSMutableArray *d;
NSMutableArray *e;
NSMutableArray *f;
NSMutableArray *g;}
- (id)init;
- (void)setDate:(NSString *)dateGio;
- (void)adda:(NSString *)i;
- (void)addb:(NSString *)i;
- (void)addc:(NSString *)i;
- (void)addd:(NSString *)i;
- (void)adee:(NSString *)i;
- (void)addf:(NSString *)i;
- (void)addg:(NSString *)i;
- (NSMutableArray *)geta;
- (NSMutableArray *)getb;
- (NSMutableArray *)getc;
- (NSMutableArray *)getd;
- (NSMutableArray *)gete;
- (NSMutableArray *)getf;
- (NSMutableArray *)getg;
@end
and this is the Item.m
#import "MealsItem.h"
@implementation MealsItem
- (id)init {
self = [super init];
if(self) {
NSString *date = @"";
NSMutableArray *a = [[NSMutableArray alloc] init];
NSMutableArray *b = [[NSMutableArray alloc] init];
NSMutableArray *c = [[NSMutableArray alloc] init];
NSMutableArray *d = [[NSMutableArray alloc] init];
NSMutableArray *e = [[NSMutableArray alloc] init];
NSMutableArray *f = [[NSMutableArray alloc] init];
NSMutableArray *g = [[NSMutableArray alloc] init];
}
return self;
}
- (void)adda:(NSString *)i {
[a addObject: i];
}
- (void)addb:(NSString *)i {
[b addObject: i];
}
- (void)addb:(NSString *)i {
[b addObject: i];
}
- (void)addd:(NSString *)i {
[d addObject: i];
}
- (void)adde:(NSString *)i {
[e addObject: i];
}
- (void)addf:(NSString *)i {
[f addObject: i];
}
- (void)addg:(NSString *)i {
[g addObject: i];
}
- (NSMutableArray *)geta {
return a;
}
- (NSMutableArray *)getb {
return b;
}
- (NSMutableArray *)getc {
return c;
}
- (NSMutableArray *)getd {
return d;
}
- (NSMutableArray *)gete {
return e;
}
- (NSMutableArray *)getf {
return f;
}
- (NSMutableArray *)getg {
return g;
}
@end
In this object i save string in the specific array. In my First view (the main of the project) i initialize the object
#import <UIKit/UIKit.h>
#import "Item.h"
@interface First_View : UIViewController {
@public Item *ok;
}
@property (nonatomic, retain) Item *ok;
@end
and in the .m
#import "First_View.h"
#import "Item.h"
@interface First_View ()
@end
@implementation First_View
@synthesize ok;
- (void)viewDidLoad
{
[super viewDidLoad];
ok = [[Item alloc] init];
}
@end
The problem is that i have to read and modify the object initialize in the First_View, using the method of "Item.m", from another class. In the other class i use this
First_View *first = [[First_View alloc] init];
and i try to modify the object but i cant. I can just if i use also
first.ok = [[Item alloc] init];
but i dont want to initialize again the object but use the object already created in the First_View. How is possible to modify the object without initialize again the object??
Upvotes: 0
Views: 138
Reputation: 52530
The code in Item.m / Item.h doesn't make any sense to me whatsoever.
You are trying to declare instance variables named 1, 2, 3, 4, 5, 6, 7. That's nonsense.
You are explicitly declaring getter and setter methods. That's just creating unnecessary work for yourself. Just use @property.
Your init method doesn't do what you think it does. It creates new objects, but doesn't assign them to instance variables, but to local variables in the init method. Which means they are lost as soon as init returns. And you tried to name these variables 1, 2, 3, 4, 5, 6, 7 which is illegal and nonsense.
Where do "breakfast" etc. come from?
Upvotes: 2
Reputation: 629
You can use what is known as lazy instantiation. If you declare your arrays as properties,
@interface Item : NSObject
@property (strong) NSMutableArray *array;
@end
XCode automatically creates the setter and getter for you. The trick is to override the getter as follows:
- (NSMutableArray *)array
{
if (!_array)
{
_array = [NSMutableArray alloc] init];
}
return _array;
}
In that way the array is only instantiated once (or zero times if you never use it – hence the word lazy). Remember that if you want to override both the setter and the getter, you must synthesize the property:
@synthesize array = _array
Upvotes: 1