Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top