0

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

13
  • You are not initializing your ok object because you are doing it in your method viewDidLoad and this method is called when the view of your controller was loaded. overwritte your method init in FirstView and add your initialization of ok object. And if you have XIB, you could to initialize your ok object in the method initWithNibName and to initialize your FirstView with this method and no with init method Commented Mar 10, 2014 at 15:04
  • Variable names cannot start with a number. Consider this: if you have a variable named 1, how would you use the integer 1? If this was allowed the compiler wouldn't know whether you meant your variable or the integer. Commented Mar 10, 2014 at 15:57
  • Thanks @SonGoku68 , I moved the code from viewDidLoad to initWithNibName. But I have another problem now. In a class (not the Fisrt_View) I add some string to the NSMutableArray using one of the set method and this work, but if i try to read, using the get method, from a third class the NSMutableArray is empty. There is the object "Item", but the NSMutableArray inside look like empty again. Commented Mar 10, 2014 at 22:32
  • I think your arrays are empty because you are not setting their properties in your .h file. Create your NSMutableArray with nonatomic and strong properties like this: @property (nonatomic, strong) NSMutableArray *a; Commented Mar 11, 2014 at 7:22
  • I tried to modify the Item.h with @property (nonatomic, strong) NSMutableArray *a; ..... but the problem is the same. The data are saved correctly in a second class but in a third other if i read the array this is (are) empty. Commented Mar 11, 2014 at 8:21

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

it will be great if you could say how he could use it in his case or give a more details example ;)
I am having troubles understanding what is going on in OP's code. I would rather suggest that he/she posted a smaller example illustrating exactly what the problem is. That increases the chances of a more precise reply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.