简体   繁体   English

读写和只读互斥

[英]readwrite and readonly mutually exclusive

How can I go about achieving this 我该如何实现这一目标

  • Property has to be readwrite for inner implementation in class 必须对类的内部实现进行属性读写

  • Property has to be readonly for external interaction to instances of the class 属性必须是只读的,以便与类的实例进行外部交互

In Objective-C: 在Objective-C中:

MyObject.h MyObject.h

@interface MyObject : NSObject

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

@end

MyObject.m MyObject.m

// Class extension
@interface MyObject ()

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

@end

@implementation MyObject

...

In Swift: 在Swift中:

class MyObject {

    private(set) var myProperty: String

    ...

Try like the following example 尝试像下面的例子

In your .h: 在您的.h中:

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

In your .m: 在您的.m中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM