Iphone 奇怪的赋值错误

Iphone 奇怪的赋值错误,iphone,objective-c,ipad,llvm-gcc,Iphone,Objective C,Ipad,Llvm Gcc,为什么会这样: - (void) setupInteraction:(IBITSInteraction*)interaction withInfo:(NSDictionary*)info { CGRect rect = ([info objectForKey:kInteractionFrameKey] ? CGRectFromString([info objectForKey:kInteractionFrameKey]) : CGRectZero); interactio

为什么会这样:

- (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
  • 错误(第二种情况):只读变量'prop.283'的赋值
  • 属性帧:
    @Property(非原子,赋值)CGRect帧
    及其相应的
    @synthesis

提前感谢。

我在GCC 4.2、LLVM GCC 4.2和Apple LLVM 2.1下的通用通知方法上尝试了这段代码。他们都没有给我错误。因此,您的Xcode安装和/或编译器安装出现了一些问题。

这是GCC4.2前端中的一个bug,在LLVM-GCC4.2中也可以重现。祝贺当所分配的值是由条件运算符生成的表达式时,这些编译器在使用点语法进行属性分配时会遇到问题

下面的代码再现了这个问题,并展示了两个源代码解决方案:一个,正如您所注意到的,是使用一个临时变量。另一种解决方案是使用传统的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,也可以避免使用点语法。您可能想提交一个bug,但我不会屏住呼吸,因为苹果不太可能更新GCC。

缺少清晰的错误消息是很难做到的,是吗?听起来像是编译器bug。你试过使用不同的编译器吗?
$ 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
$