0

Hi I have an instance variable NSMutable Array.

I declare it as such

@property (nonatomic, assign) NSMutableArray *list;

In viewDidLoad I instantiate it.

self.list = [NSMutableArray array];

I then make a string consisting of the text of text fields and add it to the array.

NSString * lines = [NSString stringWithFormat:@"%@,%@,%@,%@,%@", [self.crabText text], [self.trawlText text], [self.trapText text], [self.vesselText text], [self.lengthText text]];

    [self.list addObject:lines];

This is apart of a function which will keep on adding new values of the text fields into the array.

I display the contents of the array with

int i;
    int count;
    for (i = 0, count = [self.list count]; i < count; i = i + 1)
    {
        NSString *element = [self.list objectAtIndex:i];
        NSLog(@"The element at index %d in the array is: %@", i, element); // just replace the %@ by %d
    }

However, the app crashes when I try to print the contents of the array and I get

EXC_BAD_ACCESS_CODE

Any ideas?

Thanks!

0

2 Answers 2

3

Replace your declaration like this :

@property (nonatomic, strong) NSMutableArray *list; // strong and not assign

Initialize your array in your viewDidLoad :

self.list = [NSMutableArray array];

and add one by one your string :

[self.list addObject:self.crabText.text];
[self.list addObject:self.trawlText.text];
....

Next, modify your for loop :

for (int i = 0, i < self.list.count, i++)
{
    NSLog(@"The element at index %d in the array is: %@", i, [self.list objectAtIndex:i]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Changing assign to strong did it. But why?
because assign is for primitive variable. But, have a look here to understand how to use it stackoverflow.com/questions/8927727/…
0

Another way to do this would be to declare the array this way in your header file

@interface yourViewController : UIViewController
{
    NSMutableArray* list;
}
@end

Then in the ViewDidLoad

list = [[NSMutableArray alloc] init];

Everything else can be done just as Jordan said. Though I'm not sure if there is a difference in performance between either implementation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.