Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-c: why private ivars are not hidden from the outside access when using KVC
    primarykey
    data
    text
    <p>After trying to access ivars using KVC, I have noticed that there was no protection on private and protected ivars. It doesn't matter what I put a in front of the ivar (private or protected keyword) - an ivar is always a public ivar when using KVC method "setValue". Here is my code where all of the seven ivars and properties are changeble outside the class instance:</p> <pre><code>//************ interface file ***************// @interface MyClass : NSObject { @public NSNumber *public_num; @protected NSNumber *protected_num; @private NSNumber *private_num; NSNumber *private_property; } @property (retain) NSNumber *public_property; @property (retain) NSNumber *private_property; @end //********* implementation file *********// @interface MyClass(){ @private NSNumber *very_private_num; } @property (retain) NSNumber *very_private_property; @end @implementation MyClass @synthesize public_property, private_property, very_private_property; @end //****** main **********// MyClass *myClass = [[MyClass alloc] init]; [myClass setValue:[NSNumber numberWithInt:1] forKey:@"public_num"]; [myClass setValue:[NSNumber numberWithInt:2] forKey:@"protected_num"]; [myClass setValue:[NSNumber numberWithInt:3] forKey:@"private_num"]; [myClass setValue:[NSNumber numberWithInt:4] forKey:@"public_property"]; [myClass setValue:[NSNumber numberWithInt:5] forKey:@"private_property"]; [myClass setValue:[NSNumber numberWithInt:6] forKey:@"very_private_num"]; [myClass setValue:[NSNumber numberWithInt:7] forKey:@"very_private_property"]; NSNumber *l_public_num = [myClass valueForKey:@"public_num"]; NSNumber *l_protected_num = [myClass valueForKey:@"protected_num"]; NSNumber *l_private_num = [myClass valueForKey:@"private_num"]; NSNumber *l_public_property = [myClass valueForKey:@"public_property"]; NSNumber *l_private_property = [myClass valueForKey:@"private_property"]; NSNumber *l_very_private_num = [myClass valueForKey:@"very_private_num"]; NSNumber *l_very_private_property = [myClass valueForKey:@"very_private_property"]; NSLog(@"public_num = %@, protected_num = %@, private_num = %@, public_property = %@, private_property = %@, very_private_num = %@, very_private_property = %@", l_public_num, l_protected_num, l_private_num, l_public_property, l_private_property, l_very_private_num, l_very_private_property); </code></pre> <p>The result of the output> public_num = 1, protected_num = 2, private_num = 3, public_property = 4, private_property = 5, very_private_num = 6, very_private_property = 7. </p> <p>Even if the ivar declared at private interface, it is still changeable outside the class. So how do I have to enforce encapsulation and "to protect my ivars from evil other programmers" :) </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload