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!

4

2 回答 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]);
}
于 2013-10-20T15:37:37.727 回答
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.

于 2013-10-22T19:29:11.640 回答