Why the readonly property still allows writing with KVC

I am working on the "Key Value Coding" section in "Programming for Mac OS X". I built an interface with a slider and a label, both related to fido, int. If I set the property for fido read-only, moving the slider still forces the label to change its value. I assumed that for this I will get some kind of error. If the property is read-only, how can the slider still write the property? I thought that he would not have any setters, and KVC would not work. Thanks.

Here is the code I'm using:

#import <Cocoa/Cocoa.h>

@interface AppController : NSObject
{
    int fido;
}

@property (readonly, assign) int fido;

@end

#import "AppController.h"

@implementation AppController

@synthesize fido;

- (id)init
{
    [super init];
    [self setValue:[NSNumber numberWithInt:5] forKey:@"fido"];
    NSNumber *n = [self valueForKey:@"fido"];
    NSLog(@"fido = %@", n);
    return self;
}
@end

alt text http://idisk.me.com/nevan/Public/Pictures/Skitch/Window-20091001-174352.png

+5
3

AppController.h:

@interface AppController : NSObject
{
        int fido;
}

@property (readonly, assign) int fido;
@end

"AppController.h"

@implementation AppController
@synthesize fido;
...
@end

, AppController -fido, . -setFido:. , ""?

- (id)init
{
        if (self=[super init]) {
            [self setValue:[NSNumber numberWithInt:5] forKey:@"fido"];
            NSNumber *n = [self valueForKey:@"fido"];
            NSLog(@"fido = %@", n);
        }
        return self;
}

(BTW: -init )

, KVC , . -setValue:forKey: -setFoo:. , foo .

, fido _fido, , valueForKey 0, ( 64 , @synthesize fido. , .).

ivar bar, @synthesize foo=bar;, .

:

2009-10-01 08:59:58.081 dfkjdfkjfjkfd[24099:903] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AppController 0x20000e700> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key fido.'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00007fff85b055a4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff85c5a0f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff85b5caf9 -[NSException raise] + 9
    3   Foundation                          0x00007fff814e14f5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 434
(
    0   CoreFoundation                      0x00007fff85b055a4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff85c5a0f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff85b5caf9 -[NSException raise] + 9
    3   Foundation                          0x00007fff814e14f5 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 434
    4   dfkjdfkjfjkfd                       0x0000000100000d96 -[AppController init] + 130
+16

readonly , . - KVO/KVC.

+1

@property @synthesize - .

setter setFido:, getter fido.

When you specify readonly, I believe that it simply tells the compiler not to create a setter method, but only a getter. It does not put any barrier in the way of setting a variable in other ways.

(I hope everything is fine with me. Good luck!)

+1
source

All Articles