简体   繁体   中英

iOS - Set Derived Property

I have the following derived property:

SongWrapper.h

@property (nonatomic, strong) NSString *title;

SongWrapper.m

- (NSString *)title;
{
    return self.songDocument.songAttributes.title;
}

I tried to set it like this:

self.songViewController.songWrapper.title = titleTextField.text;

Why doesn't this work and what are the best practices for setting a derived property?

You have to defined getter but not setter. You need to define setter as well.

//Getter
- (NSString *)title;
{
    return self.songDocument.songAttributes.title;
}

//Setter
- (void)setTitle:(NSString *)title;
{
    self.songDocument.songAttributes.title = title;
}

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