1

繰り返されたオブジェクトをbyrefparamを持つメソッドに渡すforループがあり、次のエラーが発生します。

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

および警告:

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

ループ:

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

メソッドシグネチャ:

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

私はこのSOq&aでの提案に従いましたが無駄になりました。

編集

objの前に&を付けると、次のエラーが発生します。

Sending 'Foo *const __strong *' to parameter of type 'Foo *__autoreleasing *' changes retain/release properties of pointer
4

2 に答える 2

1

チャットでの議論と発見の履歴書として、ここでいくつかの考慮事項があります。

あなたがしようとしているようです:

  1. 配列の高速反復。その間

  2. ループ内から呼び出されたメソッド内の各配列要素を置き換えます。

これはコンパイラによって許可されません。少なくとも、列挙内の配列を変更しないことについての高速列挙契約を破ることになります。

したがって、私の提案は、メソッドで明示的にアウトパラメータを指定することですshouldAddObject。例:

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];
}
于 2013-01-16T17:57:41.410 に答える
1

配列はすべてポインタのコレクションにすぎないことを思い出すと、おそらくすでにこれを実行しているはずです。その場合、必要なのはshouldAddObject:を変更することだけです。

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

または、フラグを使用してそのファイルのARCをオフにします-fno-objc-arc

ただし、そうでない場合は、ARCを使用してこれを行うことができます。

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

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

 }

例:

@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

その助けのどれか?

于 2013-01-16T18:02:14.067 に答える