ios - How to create and use object with Objective-C? -
im programming objective c ios. have problem...i create class object 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 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 object save string in specific array. in first view (the main of project) initialize object
#import <uikit/uikit.h> #import "item.h" @interface first_view : uiviewcontroller { @public item *ok; } @property (nonatomic, retain) item *ok; @end
and in .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 have read , modify object initialize in first_view, using method of "item.m", class. in other class use
first_view *first = [[first_view alloc] init];
and try modify object cant. can if use
first.ok = [[item alloc] init];
but dont want initialize again object use object created in first_view. how possible modify object without initialize again object??
the code in item.m / item.h doesn't make sense me whatsoever.
you trying declare instance variables named 1, 2, 3, 4, 5, 6, 7. that's nonsense.
you explicitly declaring getter , setter methods. that's creating unnecessary work yourself. use @property.
your init method doesn't think does. creates new objects, doesn't assign them instance variables, local variables in init method. means lost init returns. , tried name these variables 1, 2, 3, 4, 5, 6, 7 illegal , nonsense.
where "breakfast" etc. come from?
Comments
Post a Comment