1
votes

I spotted this today and just want to verify my understanding of what is going on. "prefixName" is a readonly property that uses a getter method to directly pass a value back, there is no iVar storing the result on the PR_ViewController object. Also if the property was not readonly adding a setter still would not work as there is no iVar to set.

  • Created: [Meth] prefixName

By contrast "characterName" works the usual way for a property, adding a getter, a setter and an iVar.

  • Created: [Meth] characterName
  • Created: [Meth] setCharacterName
  • Created: [iVar] characterName

.

@interface PR_ViewController : UIViewController
@property (nonatomic, readonly) NSString *prefixName;
@property (nonatomic, retain) NSString *characterName;

.

@implementation PR_ViewController
@synthesize characterName;

- (NSString *)prefixName {
    return @"FRED";
}
1
Don't duplicate your questions. Please close one of this.Ilanchezhian
You're understanding it correctly.Joachim Isaksson
very strange, I am not sure what happened there as I did not post this question twice. Apologies for any confusion ...fuzzygoat
I actually can't see a duplicate here, the link to the duplicate above points back to this question. That is unless the duplicate has already been removed.fuzzygoat

1 Answers

7
votes

You are not required to synthesize accessors - you can perfectly well write them yourself, which is what is done in your example.

Further, a read-only property does not have to be based on an ivar, but can be computed, based on other ivars or properties. This can be useful if you use bindings to display values in the UI that are derived from other properties, provided you like that coding style.

Here is a simple example of a readonly property computed based on two other properties:

Header file:

@property double width;
@property double height;

@property (readonly) double area;

Implementation:

@synthesize width, height;

- (double)area
{
    return width*height;
}


+ (NSSet *)keyPathsForValuesAffectingArea
{
    return [NSSet setWithObjects:@"width", @"height", nil];
}

Now, whenever either one of width or height changes, the area property changes, too, and its changes are propagated to its listeners (thanks to keyPathsForValuesAffectingArea.)

You can also see the fullName example in this doc.