Objective-c:为什么在使用KVC时私人ivars不会被外部访问隐藏
栏目: Objective-C · 发布时间: 5年前
内容简介:翻译自:https://stackoverflow.com/questions/6122398/objective-c-why-private-ivars-are-not-hidden-from-the-outside-access-when-using
//************ 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);
搜索访问密钥值的方法,包括直接访问实例变量.这种直接访问由accessInstanceVariablesDirectly方法控制,该方法是NSKeyValueCoding非正式协议的一部分,该方法默认返回YES,允许这些方法直接访问实例变量,因此不会真正使它们成为私有.他们仍然是私人直接访问.
要解决此问题,您必须覆盖上述方法并在NSKeyValueCoding非正式协议中定义,以防止其访问.
正如Abizern所提到的,私有变量的属性仍然可以访问,因为Objective-C没有私有方法的概念.
翻译自:https://stackoverflow.com/questions/6122398/objective-c-why-private-ivars-are-not-hidden-from-the-outside-access-when-using
以上所述就是小编给大家介绍的《Objective-c:为什么在使用KVC时私人ivars不会被外部访问隐藏》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 如何拥有一个“私人泳池”
- “私人定制” CLI 工具
- 推送到私人nuget饲料提示证书
- 使用树莓派和Rocket.Chat构建一个私人聊天服务器
- 红帽开放创新实验室:为开源“水土不服”企业提供的私人定制
- NuCypher 工程师告诉你,链上私人数据如何安全存储与管理
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
HTTP Developer's Handbook
Chris Shiflett / Sams Publishing / 2003-3-29 / USD 39.99
The largest group with an unsatisfied demand for a good book on HTTP is the worldwide group of Web developers. A good book on HTTP can help new and old Web developers alike, as a thorough understandin......一起来看看 《HTTP Developer's Handbook》 这本书的介绍吧!