2011-09-16 4 views
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'の割り当て
  • プロパティフレーム:事前に、それぞれの@synthesize

おかげで@property (nonatomic, assign) CGRect frame;

+0

エラーメッセージの明瞭さが欠けているのは、達成するのが難しいですね。 – bbum

+0

コンパイラのバグのように聞こえます。あなたは別のコンパイラを使ってみましたか? –

+0

LLVM 2.0の両方のケースが動作します... – D33pN16h7

答えて

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を更新する可能性は低いので、私は息を止めません。

+0

コンパイラのバグ... awsome !!! :( – D33pN16h7

0

GCC 4.2、LLVM GCC 4.2、Apple LLVM 2.1の一般的な通知方法でこのコードを試しました。彼らの誰も私に誤りを与えませんでした。 Xcodeのインストールやコンパイラのインストールで何かが起きています。