1

My understanding of properties is that they just construct getter and setter methods. Is there a way to create a property that just has a setter method? My question is similar to this Write only property in Objective-C but it looks like the only solutions given are explicitly declaring and defining a setter method. Is that the only way or can it be done with properties?

Community
  • 1
  • 1
user1802143
  • 14,662
  • 17
  • 46
  • 55

1 Answers1

1

If you are asking if you can have IOS automatically synthesize a "write only" property, than no. However, you can explicitly set a getter method that returns nil.

Example:

@property(strong, nonatomic) NSString *myString;

-(NSString *)myString
{
    return nil;
}
JonahGabriel
  • 3,066
  • 2
  • 18
  • 28
  • Even better, use `@dynamic` to prevent a synthesized getter. Then, trying to read the property will throw an exception since the getter does not exist. Of course, this requires you to provide your own setter, but a synthesized setter probably isn't useful in the case where you don't want a getter. – ughoavgfhw Aug 22 '13 at 01:48