Mike T
Mike T

Reputation: 1163

question about readwrite properties

One thing that I like about readwrite properties is that you get KVO compliance 'for free', so I tend to use it on properties even when they are only written to from within the object to which the property belongs. On the other hand, I understand that a property should only be set to readwrite if it is intended to be writeable by other objects. So, should I use readwrite even though I only call the setter from self:

[self setFoo:bar];

The alternative (I think) is to use:

[self willChangeValueForKey:@"foo"];
foo = bar;
[self didChangeValueForKey:@"foo"];

which is an extra two lines I code I have to write every time I want to change foo. Which is better?

Upvotes: 1

Views: 113

Answers (2)

Jens Ayton
Jens Ayton

Reputation: 14558

You can declare a property readonly in the public interface, then promote it to readwrite in a class extension in the implementation file.

Foo.h:

@interface Foo: NSObject
@property (readonly) NSString *frob;
@end

Foo.m:

@interface Foo ()
@property (readwrite) NSString *frob;
@end

@implementation Foo
@synthesize frob;

// Methods in Foo.m can now use foo.frob = @"whatever";
@end

Upvotes: 3

in .h

   @property(nonatomic,readwrite,retain)NSString *foo;

then

in .m

  @synthesize foo;

then use anywhere like

  self.foo=@"madhu";

or

  self.foo=@"mike";

but if u synthesized like above then u have to use always like

self with dot

everytime while change the string

   it will automatically release the older object then retain the new one.so no pain to take care of old one for release and no pain for retain the new one.

i think its better

Upvotes: 0

Related Questions