Hockey
Hockey

Reputation: 331

How do I modify an object within a NSMutableArray


I am trying to create a basic app that displays event information from a server.
So far I download all the information in an xml file and parse it using NSXMLParser.
I have an event object that I have created that stores all of the information for each event, I then populate a NSMutableArray with these events.
I would like to then asynchronously load the images relating to each event as each UITableView Cell displays, and store them in a NSData in the event objects located within the NSMutableArray. This way I would only load images that I need.
The layout of my NSMutableArray looks like this(simplified):

events
      -event1
            -(NSString *)title
            -(NSURL *)imageURL
            -(NSData *)eventData
      -event2
            -(NSString *)title
            -(NSURL *)imageURL
            -(NSData *)eventData
etc..

I have no idea what the best practice here is and am struggling to make it work.

I get the error: expression not assignable

My question is, am I on the right track with either of these trains of thought? Can I allocate a new NSData inside of an object within a NSMutableArray? or was I on the right track asking the event object to download it's own image?
Thanks for taking the time to read this overly-long-winded question.

Upvotes: 0

Views: 158

Answers (2)

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Another alternative to modify an object within a NSMutableArray is:

if([[events objectAtIndex:cellIndexPath.row] count] >=2)
{
  [[events objectAtIndex:cellIndexPath.row] replaceObjectAtIndex:2  withObject:[[NSData alloc] initWithContentsOfURL:[[events objectAtIndex:cellIndexPath.row] eventImgURL]];
}

Upvotes: 1

user529758
user529758

Reputation:

You can't use a getter method to set a property, you have to use a setter method:

[[events objectAtIndex:cellIndexPath.row] setEventImageData:[[NSData alloc] initWithContentsOfURL:[[events objectAtIndex:cellIndexPath.row] eventImgURL]]];

Upvotes: 3

Related Questions