1

I've got a for loop that then passes the iterated object into a method with a byref param and get the following error:

Implicit conversion of an Objective-C pointer to 'FOO *__autoreleasing *' is disallowed with ARC

and warning:

Incompatible pointer types sending 'Foo *const __strong' to parameter of type 'Foo *__autoreleasing *'

The Loop:

for (Foo *obj in objArray) {
    FooTableCell *newCell = [self createFooCellWithItem:obj];
}

The Method Signature:

-(FooTableCell *)createFooCellWithItem:(Foo **)newObj;

I've followed the suggestions at this SO q&a to no avail.

EDIT

Prepending a & before obj gives me the following error:

Sending 'Foo *const __strong *' to parameter of type 'Foo *__autoreleasing *' changes retain/release properties of pointer
Community
  • 1
  • 1
sbonami
  • 1,892
  • 17
  • 33

2 Answers2

1

As a resume of the discussion and findings in chat, here a couple of considerations.

It seems that you are trying to:

  1. fast iterating over an array; while

  2. replacing each array element within a method called from inside the loop;

this will not be allowed by the compiler. It would at least break the fast enumeration contract about not modifying the array within the enumeration.

My suggestion thus is specify explicitly an out-parameter in your shouldAddObject method, e.g.:

NSMutableArray *newArray = [[NSMutableArray alloc] initWithCapacity:[objArray count]]; 
for (Foo *obj in objArray) {
    Foo* newObject = nil;
    RETYPE* ret = [self shouldAddObject:obj newObject:&newObject]; 
    [newArray addObject:newObject];
}
sergio
  • 68,819
  • 11
  • 102
  • 123
  • I tried that and I get the following error: Sending 'Foo *const __strong *' to parameter of type 'Foo *__autoreleasing *' changes retain/release properties of pointer – sbonami Jan 16 '13 at 18:01
  • Now ARC is just being a pain: Incompatible types casting 'Foo *const __strong *' to 'Foo *__strong *' with a __bridge cast – sbonami Jan 16 '13 at 18:05
  • Does it have to do with how ObjC creates the iterator obj in a loop? – sbonami Jan 16 '13 at 18:06
  • a question: why are you passing a Foo**? are you allocating the object inside `shouldAddObject`? – sergio Jan 16 '13 at 18:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22851/discussion-between-scott-bonami-and-sergio) – sbonami Jan 16 '13 at 18:26
  • Perfect, thanks a ton for sticking around in the chat to help figure it out! – sbonami Jan 16 '13 at 20:07
1

If I recall arrays are all just collections of pointers, so you're probably doing this already, in which case all you need is to change your shouldAddObject:

 -(void)shouldAddObject:(Foo *)newObj {
      // Do your thing
 }

or turn off ARC for that file using the flag -fno-objc-arc.

However, if that's not the case you can do this with ARC:

 - (void)swapObjCPointers:(id*)ptrA with:(id*)ptrB {

     // Puts pointer B into pointer A
     id this = *ptrB;
     *ptrB = *ptrA;
     *ptrA = this;

 }

Example:

@implementation MyARCFile

 - (void)swapObjCPointers:(id*)ptrA with:(id*)ptrB {

     // Puts pointer B into pointer A
     id this = *ptrB;
     *ptrB = *ptrA;
     *ptrA = this;

 }


- (void)example {

     id objA = [NSObject new];
     id objB = @"String";

     NSLog(@"\n"
           @"a: %p\n"
           @"b: %p\n",
           objA,
           objB);

     [self swapObjCPointers:&objA with:&objB];

     NSLog(@"\n"
           @"a: %p\n"
           @"b: %p\n",
           objA,
           objB);
 }

 @end

Any of that help?

Miles Alden
  • 1,542
  • 1
  • 11
  • 21