2

これが機能する理由:

- (void) setupInteraction:(IBITSInteraction*)interaction withInfo:(NSDictionary*)info
{    
    CGRect rect = ([info objectForKey:kInteractionFrameKey] ? CGRectFromString([info objectForKey:kInteractionFrameKey]) : CGRectZero);
    interaction.frame = rect;
    ...
}

そして、なぜこれはしないのですか?:

- (void) setupInteraction:(IBITSInteraction*)interaction withInfo:(NSDictionary*)info
{    
    interaction.frame = ([info objectForKey:kInteractionFrameKey] ? CGRectFromString([info objectForKey:kInteractionFrameKey]) : CGRectZero);
    ...
}

全く同じだと思います…

  • コンパイラ: LLVM GCC 4.2
  • エラー (2 番目のケース): 読み取り専用変数 'prop.283' の割り当て
  • プロパティ フレーム:@property (nonatomic, assign) CGRect frame;それぞれの@synthesize

前もって感謝します。

4

2 に答える 2

3

これは GCC 4.2 フロントエンドのバグであり、LLVM-GCC 4.2 でも再現可能です。おめでとう!これらのコンパイラでは、割り当てられる値が条件演算子の結果の式である場合、ドット構文を使用したプロパティの割り当てに問題があります。

次のコードは問題を再現し、2 つのソース コード ソリューションを示しています。もう 1 つの解決策は、ドット構文ではなく、従来の Objective-C メッセージ送信構文を使用することです。

#import <Foundation/Foundation.h>

CGRect CGRectFromString(NSString *string);

@interface SomeClass : NSObject
@property (nonatomic, assign) CGRect frame;
@end

@implementation SomeClass
@synthesize frame;
@end

int main(void) {
    NSDictionary *info;
    SomeClass *interaction;
    NSString *kInteractionFrameKey;

    CGRect rect;
    rect = [info objectForKey:kInteractionFrameKey] ? CGRectFromString([info objectForKey:kInteractionFrameKey]) : CGRectZero;
    interaction.frame = rect;

    [interaction setFrame:([info objectForKey:kInteractionFrameKey] ? CGRectFromString([info objectForKey:kInteractionFrameKey]) : CGRectZero)];

    interaction.frame = ([info objectForKey:kInteractionFrameKey] ? CGRectFromString([info objectForKey:kInteractionFrameKey]) : CGRectZero);
    interaction.frame = ([info objectForKey:kInteractionFrameKey] ? CGRectZero : CGRectFromString([info objectForKey:kInteractionFrameKey]));
    interaction.frame = ([info objectForKey:kInteractionFrameKey] ? CGRectZero : CGRectZero);

    return 0;
}

さまざまなコンパイラでテストすると、次の結果が得られます。

$ llvm-gcc -c test.m
test.m: In function ‘main’:
test.m:24: error: assignment of read-only variable ‘prop.76’
test.m:25: error: assignment of read-only variable ‘prop.77’
test.m:26: error: assignment of read-only variable ‘prop.78’

$ clang -c test.m
$

その特定のケースでは、LLVM を使用するか、ドット構文を避けることができます。バグを報告したくなるかもしれませんが、Apple が GCC を更新する可能性は低いので、息を止めません。

于 2011-09-17T01:35:00.100 に答える
0

GCC 4.2、LLVM GCC 4.2、および Apple LLVM 2.1 の下で、一般的な通知方法でこのコードを試しました。それらのどれも私にエラーを与えませんでした。そのため、Xcode のインストールやコンパイラのインストールで何かが起こっています。

于 2011-09-16T23:13:14.877 に答える