简体   繁体   English

iOS-设置派生属性

[英]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. 您必须定义getter而不是setter。 You need to define setter as well. 您还需要定义setter。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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