In my app i use

[theMutableArray addObject:theString];

several times and it works every time, however when trying to add in another view it won't add.

I know my string is not null because i have

NSLog(@"%@, %@", theString, theMutableArray);

and it returns "the string value", (null)

In my viewDidLoad i have

theMutableArray = [[NSMutableArray alloc] init];

and where i try to add it I have

theString = [alertView textFieldAtIndex:0].text;
[theMutableArray addObject:theString];
NSLog(@"%@, %@", theString, theMutableArray);

Is there a typo of any sort? or did i forget something?

有帮助吗?

解决方案 3

I added these 3 lines and it worked, i probably has something wrong with the array somewhere else.

 NSMutableArray *newMutableArray = [[NSMutableArray alloc] init];
[newMutableArray addObject:theString];

theMutableArray = newMutableArray;

其他提示

You said that

NSLog(@"%@, %@", theString, theMutableArray);

returns "the string value", (null). So what do you expect? Your theMutableArray seems to be nil and this is a reason why you can not add anything to it.

Make this a property of your class so that you can access it across the lifetime of its parent object and not just within one method. (I presume you want to access it in multiple methods).

@interface myClass : NSObject
@property (nonatomic) NSMutableArray *theMutableArray;
@end

@implementation myClass

- (void) viewDidLoad {
    self.theMutableArray = [NSMutableArray new];
}

- (IBAction) anActionDidHappen:(id) sender {
    …
    theString = [alertView textFieldAtIndex:0].text;

    [self.theMutableArray addObject:theString];
}

@end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top