在iOS中是否可以使用KVC将值设置为只读属性?

时间:2022-09-11 20:50:36

I am trying to describe my confusion here. Let me know if this question need any modification.

我试图在这里描述我的困惑。如果这个问题需要修改,请告诉我。

Accordind to Apple documentaition: 在iOS中是否可以使用KVC将值设置为只读属性?

苹果documentaition Accordind:

Apple documentation link

苹果的文档链接

So we can't set value to readonly property because it's setter will not created. But I have created a demo project for testing this. I am sharing my code sample with you.

我们不能将value设置为readonly属性因为它的setter不会创建。但是我已经创建了一个测试这个的演示项目。我正在与您分享我的代码示例。

This is "ModelTest1.h" class. It has a readonly property.

这是“ModelTest1。h”类。它有一个只读属性。

#import <Foundation/Foundation.h>

@interface ModelTest1 : NSObject

@property (readonly) NSMutableArray  *arrTest;

@end

Now I am setting value in it using KVC. Because this property is readonly, so at runtime it should give me an error but it is not happening.

现在我用KVC设置它的值。因为这个属性是只读的,所以在运行时它会给我一个错误,但是它不会发生。

- (void)readonlyTest
{
    ModelTest1  *obj1 = [ModelTest1 new];

    NSLog(@"obj1.arrTest before = %@",obj1.arrTest);

    [obj1 setValue:[NSMutableArray new] forKey:@"arrTest"];

    NSLog(@"obj1.arrTest after = %@",obj1.arrTest);

    [obj1.arrTest addObject:@"Str 1"];

    NSLog(@"obj1.arrTest after 2 = %@",obj1.arrTest);

    [obj1 release];
}

Output is:

输出是:

在iOS中是否可以使用KVC将值设置为只读属性?

I am not getting one thing here that why memory is getting allocated in NSMutableArray even it is readonly. And why a string is adding into this readonly array.

我在这里并没有得到一件事情,那就是为什么内存在NSMutableArray中被分配,即使它是只读的。以及为什么一个字符串要添加到这个只读数组中。

I did this memory allocation test with NSString also using KVC and got same result. Value was set to readonly string using KVC.

我用NSString做了内存分配测试也用KVC,得到了同样的结果。使用KVC将值设置为只读字符串。

This is my confusion regarding setValue for readonly property.

这是我对于readonly属性的setValue的混淆。

So am I misunderstood something about readonly property? or there is something else going on?

那么我是否误解了readonly属性?或者还有别的事情发生?

1 个解决方案

#1


10  

The documentation you cite talks specifically about a setter method. That is not the same as KVC. KVC, by default, sets the underlying instance variable directly if it can't find a setter method to call. So, readonly suppresses synthesis of the setter method, but that doesn't stop KVC.

您引用的文档专门讨论setter方法。这和KVC不一样。在默认情况下,KVC如果无法找到调用的setter方法,则直接设置底层实例变量。所以,readonly抑制setter方法的合成,但这并不能阻止KVC。

You can turn off this feature (KVC's ability to go straight to the instance variable) by setting a class's accessInstanceVariablesDirectly to NO, but the default is YES.

通过将类的accessinstancevariablesdirect设置为NO,您可以关闭这个特性(KVC可以直接访问实例变量),但默认值是YES。

#1


10  

The documentation you cite talks specifically about a setter method. That is not the same as KVC. KVC, by default, sets the underlying instance variable directly if it can't find a setter method to call. So, readonly suppresses synthesis of the setter method, but that doesn't stop KVC.

您引用的文档专门讨论setter方法。这和KVC不一样。在默认情况下,KVC如果无法找到调用的setter方法,则直接设置底层实例变量。所以,readonly抑制setter方法的合成,但这并不能阻止KVC。

You can turn off this feature (KVC's ability to go straight to the instance variable) by setting a class's accessInstanceVariablesDirectly to NO, but the default is YES.

通过将类的accessinstancevariablesdirect设置为NO,您可以关闭这个特性(KVC可以直接访问实例变量),但默认值是YES。