How to create and use an object with Objective-C?

advertisements

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??


The code in Item.m / Item.h doesn't make any sense to me whatsoever.

  1. You are trying to declare instance variables named 1, 2, 3, 4, 5, 6, 7. That's nonsense.

  2. You are explicitly declaring getter and setter methods. That's just creating unnecessary work for yourself. Just use @property.

  3. 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.

  4. Where do "breakfast" etc. come from?