如何使用ARC在iOS中将对象标记为Retain和Readonly?

时间:2022-09-07 10:55:11

I use the following to expose languages array.

我使用以下方法来公开语言数组。

@property(nonatomic,readonly)NSArray *languages;

Assigning languages before ARC was like this:

在ARC之前分配语言是这样的:

languages=[[NSArray arrayWithObjects:
               [[Language alloc]initWithCode:@"es"],
               [[Language alloc]initWithCode:@"en"],
               nil] retain];

So, I was both able to retain the object and also mark it as readonly to outside.

所以,我既能保留对象,又能将其标记为外部的只读对象。

With ARC, As I cannot type "retain" manually. How can I do this without overriding setters and getters? Is there a way to mark a property both readonly (to outside) and retain (to inside) for ARC?

使用ARC,因为我无法手动输入“保留”。如果不覆盖setter和getter,我怎么能这样做呢?是否有办法为ARC标记一个属性readonly(to outside)和retain(to inside)?

1 个解决方案

#1


3  

retain* and readonly really have nothing to do with each other. readonly indicates that the property only has a getter, not a setter. retain means that the class maintains a retaining (strong) reference to the object referenced by the property, and under ARC, assuming the property is backed by an underlying, synthesized instance variable, means that the instance variable is a __strong variable.

retain *和readonly实际上彼此无关。 readonly表示该属性只有一个getter,而不是一个setter。 retain表示类维护对属性引用的对象的保留(强)引用,并且在ARC下,假设该属性由基础合成实例变量支持,则表示实例变量是__strong变量。

If you want a property that is read-only to the outside world, but can be read and written inside the class, you can redeclare the property as readwrite in a class extension in your implementation (.m) file. In the header:

如果您想要一个对外部只读,但可以在类中读取和写入的属性,则可以在实现(.m)文件的类扩展中将该属性重新声明为readwrite。在标题中:

@property (nonatomic, strong, readonly) NSArray *languages;

Then, at the top of your .m:

然后,在.m的顶部:

@interface YourClass ()
    @property (nonatomic, strong, readwrite) NSArray *languages;
@end

Inside the class's implementation, you can now use self.languages = ...;, (or _languages = ...; in the initializer) to set the languages property. This is not actually different under ARC vs. non-ARC...

在类的实现中,您现在可以使用self.languages = ...;,(或初始化程序中的_languages = ...;)来设置languages属性。在ARC与非ARC之间,这实际上并没有什么不同......

*Under ARC, it's more customary to use strong instead of retain, but the compiler treats them the same way.

*在ARC下,使用strong而不是retain更为习惯,但编译器会以相同的方式处理它们。

#1


3  

retain* and readonly really have nothing to do with each other. readonly indicates that the property only has a getter, not a setter. retain means that the class maintains a retaining (strong) reference to the object referenced by the property, and under ARC, assuming the property is backed by an underlying, synthesized instance variable, means that the instance variable is a __strong variable.

retain *和readonly实际上彼此无关。 readonly表示该属性只有一个getter,而不是一个setter。 retain表示类维护对属性引用的对象的保留(强)引用,并且在ARC下,假设该属性由基础合成实例变量支持,则表示实例变量是__strong变量。

If you want a property that is read-only to the outside world, but can be read and written inside the class, you can redeclare the property as readwrite in a class extension in your implementation (.m) file. In the header:

如果您想要一个对外部只读,但可以在类中读取和写入的属性,则可以在实现(.m)文件的类扩展中将该属性重新声明为readwrite。在标题中:

@property (nonatomic, strong, readonly) NSArray *languages;

Then, at the top of your .m:

然后,在.m的顶部:

@interface YourClass ()
    @property (nonatomic, strong, readwrite) NSArray *languages;
@end

Inside the class's implementation, you can now use self.languages = ...;, (or _languages = ...; in the initializer) to set the languages property. This is not actually different under ARC vs. non-ARC...

在类的实现中,您现在可以使用self.languages = ...;,(或初始化程序中的_languages = ...;)来设置languages属性。在ARC与非ARC之间,这实际上并没有什么不同......

*Under ARC, it's more customary to use strong instead of retain, but the compiler treats them the same way.

*在ARC下,使用strong而不是retain更为习惯,但编译器会以相同的方式处理它们。