简体   繁体   中英

Property, iVar, accessor method…?

I have a couple of classes that I set up properties in.

eg a class called MyDetailCell that subclassed UITableViewCell .

In it I had a property something like...

@property SomeClass *someValue;

Initially I was using it and storing it within the setter function...

- (void)setSomeValue:(SomeClass*)someValue
{
    _somValue = someValue;

    //do something with the _someValue object like changing text or something...
}

Anyway, I decided that I didn't actually need to store the value that was sent in so I was just doing this...

- (void)setSomeValue:(SomeClass*)someValue
{
    //do something with the _someValue object like changing text or something...
}

And then I changed to where I was just passing the setter forward to a subview. Like this...

- (void)setSomeValue:(SomeClass*)someValue
{
    self.someSubView.someValue = someValue;
}

So now I'm not sure what to use. I'm not actually saving the property at all now so I guess I don't really need it. I don't need to access it to get the value either I just need to set it.

At the moment I've left the property in there as it is.

  • Is that OK?
  • Is that bad practise?
  • Should I change it for just a setter method instead?
  • What are the benefits of doing that?

Is that OK?

No.

Is it bad practice?

Yes.

Should I change it for a just a setter method instead? What are the benefits of doing that?

I don't see what you mean by this. You wrote 'just a setter method' and you also did some processing in the setter method, which is fine, but you did other things wrong. One is that you don't retain a non-delegate object - you should. You don't provide a getter - that's also a broken concept. There can be readonly and readwrite properties, but not write only properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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