簡體   English   中英

讀寫和只讀互斥

[英]readwrite and readonly mutually exclusive

我該如何實現這一目標

  • 必須對類的內部實現進行屬性讀寫

  • 屬性必須是只讀的,以便與類的實例進行外部交互

在Objective-C中:

MyObject.h

@interface MyObject : NSObject

@property (nonatomic, readonly, strong) NSString *myProperty;

@end

MyObject.m

// Class extension
@interface MyObject ()

// Redeclare property read-write
@property (nonatomic, readwrite, strong) NSString *myProperty;

@end

@implementation MyObject

...

在Swift中:

class MyObject {

    private(set) var myProperty: String

    ...

嘗試像下面的例子

在您的.h中:

@property(nonatomic, retain, readonly) NSDate* theDate;

在您的.m中:

@interface TheClassName()
@property(nonatomic, retain, readwrite) NSDate* theDate;
@end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM