奇怪的赋值错误

发布于 2024-12-05 13:04:18 字数 787 浏览 1 评论 0原文

为什么这有效:

- (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”的赋值
  • 属性框架:@属性(非原子,分配)CGRect 框架; 及其各自的 @synthesize

提前致谢。

Why this works:

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

and why this doesn't?:

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

I think is exactly the same...

  • Compiler: LLVM GCC 4.2
  • Error (second case): Assignment of read-only variable 'prop.283'
  • Property frame: @property (nonatomic, assign) CGRect frame; with its respective @synthesize

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴梦一场 2024-12-12 13:04:18

这是 GCC 4.2 前端中的一个错误,也可以在 LLVM-GCC 4.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-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 或避免点语法。你可能想提交一个 bug,但我不会屏住呼吸,因为 Apple 不太可能更新 GCC。

It is a bug in the GCC 4.2 frontend, also reproducible in LLVM-GCC 4.2. Congratulations! These compilers have problems in property assignments using dot syntax when the value being assigned is an expression resulting from the conditional operator.

The following code reproduces the problem and shows two source code solutions: one, as you’ve noticed, is to use a temporary variable. The other solution is to use traditional Objective-C message sending syntax instead of dot syntax:

#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;
}

Testing with different compilers yields the following:

$ 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
$

You can either use LLVM or avoid dot syntax in that particular case. You might want to file a bug but I wouldn’t hold my breath since Apple isn’t likely to update GCC.

宛菡 2024-12-12 13:04:18

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

I tried this code on a generic notification method under GCC 4.2, LLVM GCC 4.2 and Apple LLVM 2.1. None of them gave me errors. So, something is going on with your Xcode installation and/or compiler installation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文