ios - 如何在子类中设置只读属性?

标签 ios iphone objective-c inheritance parse-platform

MyLoginViewControllerPFLogInViewController 的子类。

logInViewControllerMainViewController.m

MyLogInViewController 类型的对象

//  MainViewController.m

    MyLogInViewController *logInViewController = [[MyLogInViewController alloc] init];
            logInViewController.delegate = self;
            logInViewController.fields = PFLogInFieldsUsernameAndPassword | 
                                                     PFLogInFieldsTwitter | 
                                                    PFLogInFieldsFacebook | 
                                                PFLogInFieldsSignUpButton | 
                                                PFLogInFieldsDismissButton;

如您所见,在 MainViewController.m 中设置了 fields 属性

我想在 MyLogInViewController 中设置它,以便每个 MyLogInViewController 对象都设置它。

但是,fields 的类型是readonly 从中可以看出:https://www.parse.com/docs/ios/api/Classes/PFLogInView.html#//api/name/fields

@property (nonatomic, readonly, assign) PFLogInFields 字段

  1. 如何覆盖它? (我考虑过使用自定义 getter - 但不知道如何正确执行)

  2. 我尝试做的事情是否被视为不良做法,如果是,为什么?

编辑:原来我在看View文档不是 ViewController文档。感谢 mrt 发现这一点。

最佳答案

如果有人做了只读的事情,并且他们知道自己在做什么,那可能意味着它真的不应该设置在类之外。但是,可能仍然有一些理由去做你想做的事。为什么不在 init 方法中这样做呢?

编辑:

可以设置 PFLogInViewController 中的 fields 属性。所以你可以在你的 MyLogInViewController 类初始化方法中做到这一点

- (id)init
{
    self = [super init];
    if (self) {
        // Do aditional init stuff
        self.fields = PFLogInFieldsUsernameAndPassword | PFLogInFieldsTwitter | PFLogInFieldsFacebook | PFLogInFieldsSignUpButton | PFLogInFieldsDismissButton;
    }
    return self;
}

这样,您就不会干扰类的内部结构。

关于ios - 如何在子类中设置只读属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20407759/

相关文章:

ios - iOS 设备之间的 TCP 连接

iphone - 自定义 UITableViewCell 子类 : This class is not a key value coding-compliant

iphone - 旋转图像突然翻转

iphone - 适用于 iOS 的 Xcode 项目自动化

iphone - 从 iphone 应用程序删除 ftp 服务器上的文件

iPhone FTP 集成

ios - ios Objective C 应用程序上的 AVPlayer 无法播放视频

objective-c - NSTimer 触发 UIProgressView 失败

ios - 阅读IOS6中的请勿打扰设置

iphone - 如何在 iPhone sdk 中传递 float 数据类型的值?