0

*emphasized text*So I'm using restkit to pull back a bunch of objects to display in a UI Collection view. I get my initial mapping result, which is an NSArray of MyCell Objects.

I invoke the given success block and return the mappingResult.array to the caller of my getCellObjects service method.

Now in the calling viewController I just want to set my local NSArray property that drives my UICollectionView to the one I go from restkit's mapping.

[[MyService sharedClient] getCellObjectsWithSuccess:^(RKObjectRequestOperation *op, RKMappingResult mappingResult){
          self.myCells = [[NSMutableArray alloc] initWithArray:mappingResult.array];
}

In the example, self.MyCells is an NSMutableArray and mappingResult.array is an NSArray.

I'm new to objective-c. Is there a way to set result array from restKit to my local NSArray property?

The goal is that once the call to getCellObjects is done, I don't need the mappingResult.array anymore so it should be discarded and it's contents should be transferred over to self.MyCells.

The error I get on the call to initWithArray is Terminating app due to uncaught exception 'NSInvalidArgumentException', reason'-[__NSArrayM array]: unrecognized selector sent to instance [some memory address]

topwik
  • 3,487
  • 8
  • 41
  • 65

2 Answers2

3

mappingResult is already an array, so why do you need to call the (hypothetical, because it doesn't exist for the NSArray class) array method?

This would be fine:

self.MyCells = [[NSMutableArray alloc] initWithArray:mappingResult];

But I remember that there is also a mutableCopy method, suitable for when you need to get a mutable copy of the object (the result is identical):

self.MyCells= [mappingResult mutableCopy];

I also would like to remember that by convention all ivars need to start with a non capital letter, so I suggest to change MyCells to myCells.

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
  • Welp, you actually were right, my good fellow. I was sending result.array so the object I was getting in my callback block was already the array! thanks. – topwik May 25 '13 at 00:35
  • Note if your property myCells has retain or strong attribute you will get memory leak. You should release mutablecopy or add autorelease method. – stosha May 25 '13 at 00:52
  • @stosha I assumed he's using ARC. – Ramy Al Zuhouri May 25 '13 at 11:22
1

mappingResult is already an array, so you don't need mappingResult.array

Call mutableCopy method on an NSArray object to obtain a mutable copy of that array

self.MyCells= [mappingResult mutableCopy];

Also, MyCells is an ivar so according to variable naming convention you I suggest you name it myCells.

HERE YOU GO: self.myCells= [mappingResult mutableCopy];

  • why then do the restKit folks expose an accessor called array? i tried doing mappingResult mutableCopy and still got the selector error. I changed it to mappingResult.array mutableCopy and it goes through. remember mappingResult in my example is an RKMappingResult. I don't see that RKMappingResult implements NSArray or anything. I am using Restkit 0.20.0 – topwik May 27 '13 at 19:38
  • The warning "[__NSArrayM array]: unrecognized selector sent to instance [some memory address]" shows how it is an instance of a mutable array. Post again if you're still having problems. – Ramy Al Zuhouri May 28 '13 at 11:37
  • i ended up using self.items = [mappingResult.array mutableCopy]. Calling mutable copy on mappingResult directly gave me the selector error. – topwik May 28 '13 at 18:22