3

I'm working on an app that needs to retrieve some data from a server. I have created a "Server" class which handles all the communication and has a NSMutableArray *sessionData variable where I would like to store the data coming from the server (btw, is this approach correct?).

I have the data in an NSArray. I would like the NSMutableArray to have the same content of the NSArray but I didn't find any way to do this (sessionData = requestResult).

(subquestion: do I have to initialize in some way the NSMutableArray before using ? I have only declared it with @property and @synthesize)

1
  • I have tried this but didnt work: [sessionData removeAllObjects]; [sessionData setArray:result]; Commented Mar 30, 2012 at 12:44

5 Answers 5

5

The code you tried (from the comment) should have worked. The reason it did not work is that your sessionData was nil.

You need to initialize your sessionData - set it to [NSMutableArray array] in the initializer; then your code

[sessionData removeAllObjects];
[sessionData setArray:result];

will work perfectly. You do not even need the first line - the second one replaces the content of sessionData with that of the result.

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

2 Comments

Thank you. Is this code good to initialize it (from the the init method of my class) ? self->sessionData = [[NSMutableArray alloc] init];
@anthmad Yes, this initialization code should work. If you are not on ARC, consider assigning to a property, rather than to its backing ivar; if you're on ARC, it does not matter.
4

Try this way:

sessionData = [result mutableCopy];
[result release];

Or

NSMutableArray *sessionData = [[NSMutableArray alloc] initWithContentsOfArray:result];

Comments

0

Or, if you could do this:

NSMutableArray *session = [NSMutableArray arrayWithArray:someArray];

Comments

0

1. is this approach correct?

Yes.

2. I didn't find any way to do this (sessionData = requestResult)

As many have suggested you can use mutableCopy to assign requestResult to sessionData OR you can use arrayWithArray as one answer suggests.

3. do I have to initialize in some way the NSMutableArray before using ?

Yes. If you are changing any variable it must have memory allocated.

Comments

0

In your example something like this:

NSArray *requestData = [[NSArray alloc] initWithObjects:@"3", @"4", @"5", nil];
_sessionData = [[NSMutableArray alloc] initWithArray:requestData];
[requestData release];

NSLog(@"%@", [sessionData objectAtIndex:0]); // 2012-03-30 15:53:39.446 <app name>[597:f803] 3
NSLog(@"count: %d", [sessionData count]); //2012-03-30 15:53:39.449 <app name>[597:f803] count: 3

1 Comment

sessionData is a property that I need to access from other methods. Your code will create a local mutable array ?

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.