Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
458 views
in Technique[技术] by (71.8m points)

cocoa - Objective-C : (private / public properties) making a property readonly for outside class calls and readwrite for self calls

Would you know a way to make a property readonly for outside calls and readwrite for inside calls ?

I've read times ago somthing that seemed like

In the .h

@property(nonatomic, readonly) NSDate* theDate;

In the .m

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

but this raises a warning when compiling the .m "Property theDate attribute in TheClassName class continuation does not match class TheClassName property".

Anyway, it seems to work (can read but not set from outside the class, can do both from inside) but I should have missed somehting to avoid the warning. Or if you know a better way to do this...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your .h:

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

In your .m:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...