Question

From Xcode 4.4 onwards has Default Synthesis Of Properties. It generates this automatically:

  @synthesize name = _name;

source

And from source2

readwrite vs readonly determines whether a synthesized property has a synthesized accessor or not (readwrite has a setter and is the default, readonly does not).

Therefore, I've concluded that @synthesize name = _name; is not required for readwrite but it's needed for readonly

However, in Apple's spritekit Adventure code (adventure code download link), APAAdventureScene.m:

"heroes" (readwrite) is synthesize in the example. If it's not synthesize it will give this error: Use of undeclared identifier '_heroes'

Is @synthesize required for readwrite property, I'm confuse?

Thank you

 @interface APAAdventureScene () <SKPhysicsContactDelegate>
...

@property (nonatomic, readwrite) NSMutableArray *heroes;  // our fearless adventurers

@property (nonatomic) NSMutableArray *goblinCaves;        // whence cometh goblins

...
@end



@implementation APAAdventureScene

@synthesize heroes = _heroes;

- (id)initWithSize:(CGSize)size {
...
        _heroes = [[NSMutableArray alloc] init];

        _goblinCaves = [[NSMutableArray alloc] init];
...
}

- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {

    // Update all players' heroes.

    for (APAHeroCharacter *hero in self.heroes) {

        [hero updateWithTimeSinceLastUpdate:timeSinceLast];

    }

    // Update the caves (and in turn, their goblins).

    for (APACave *cave in self.goblinCaves) {

        [cave updateWithTimeSinceLastUpdate:timeSinceLast];

    }

}

@end
Was it helpful?

Solution

@synthesize isn't required for anything anymore as long as you are using a modern LLVM compiler (the default for over 1 year now).

readwrite is the default so both properties are read/write. There is NO reason for the @synthesize line in the posted code.

The only exception to this is if you explicitly provide both the "setter" and "getter" for a readwrite property. Then the ivar is not automatically generated. For a readonly property the ivar isn't generated if you supply an explicit "getter".

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