sách gpt4 ăn đã đi

objective-c - Lỗi thuộc tính (property): Tổng hợp thuộc tính tự động sẽ không tổng hợp thuộc tính

In lại 作者:太空狗 更新时间:2023-10-30 03:16:46 30 4
mua khóa gpt4 giày nike

bản tóm tắt:

这个问题是关于属性的继承与从彼此继承属性的类的内部和外部的不同读/写访问相结合。

详细信息:

我有一个类 MỘT 和另一个继承自 MỘT 的类 BMỘT 中声明了属性someProperty。我希望该属性在这些类外部是只读的,而在内部是读/写的。

只有一个类,这非常简单:您将 .h 中的属性声明为只读,然后在类别内的 .m 中再次将其声明为可读写。完成。

但是对于两个类,一个派生自另一个,我在 B 中收到以下编译器警告:

Auto property synthesis will not synthesize property 'someProperty' because it is 'readwrite' but it will be synthesized 'readonly' via another property

Mã này như sau:

嗯:

#import 

@interface A : NSObject

// This property shall be readonly from outside, but read/write from subclasses
@property (readonly) SInt32 someProperty;

@kết thúc

上午:

#import "A.h"

@implementation A
@kết thúc

B.h:

#import 
#import "A.h"

@interface B : A

@kết thúc

B.m:

#import "B.h"    

@interface B ()

// compiler warning in the following property declaration:
// /Users/.../B.m:12:41: Auto property synthesis will not synthesize property
// 'someProperty' because it is 'readwrite' but it will be synthesized
// 'readonly' via another property
@property (readwrite) SInt32 someProperty;

@kết thúc

@implementation B
@kết thúc

为什么会出现此警告?我应该如何构建代码以避免出现此警告?

câu trả lời hay nhất

您需要在所属类 (MỘT) 上将该属性声明为可读写,然后在子类 (B) 上重新声明,以使编译器意识到你想在那里使用它。因此,MỘT 托管访问器方法,B 使用它。通常您不希望 B 创建另一个访问器方法,因此您可以使用 @năng động 告诉编译器父类(super class)(技术上,只是另一个类)将提供实现.

请注意,您还可以在 MỘT 中声明一个类别(不是扩展名),在 B.m 中显式声明访问器方法(不使用属性,只是一个方法),因为那是您真正感兴趣的(您实际上不想要属性指定的任何其他东西,并且您真的不想要确保属性特性在父类(super class)和子类中匹配的维护开销)...

关于objective-c - 属性(property)继承 : Auto property synthesis will not synthesize property,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24570559/

30 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress