Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何重写该类属性?_Ios_Swift - Fatal编程技术网

Ios 如何重写该类属性?

Ios 如何重写该类属性?,ios,swift,Ios,Swift,如果我将其子类化,如何覆盖部分?我想更改标题 这不能建立 struct SectionData { var items : [LiveCellObjectProtocol] var title: String? = nil subscript(index: Int) -> LiveCellObjectProtocol { return items[index] } } extension SectionData{ init(tit

如果我将其子类化,如何覆盖
部分
?我想更改标题

这不能建立

struct SectionData {
    var items : [LiveCellObjectProtocol] 
    var title: String? = nil
    subscript(index: Int) -> LiveCellObjectProtocol {
        return items[index]
    }
}

extension SectionData{
    init(title: String? = nil){
        self.title = title
        self.items = []
    }
}

class LiveCellTableViewController: UIViewController  {

    var sections: [SectionData] = {
        return [SectionData(title: "aaa"), SectionData(title: "bbb"), SectionData(title: "ccc")]
    }()
}

覆盖
属性不能是存储属性。这正是斯威夫特的工作方式(如果你仔细想想,这是有道理的)。我在下面列出了各种可能性:

  • 如果超类属性是可写的(存储属性或带有setter的计算属性),子类的重写可能包括 将setter观察器添加到此属性的步骤

  • 或者,子类的重写可以是计算属性。在这种情况下:

    • 如果存储了超类属性,则子类的 属性重写必须同时具有getter和setter

    • 如果计算了超类属性,则子类的 属性重写必须重新实现 超类实现。如果超类属性是只读的(它具有 只是一个getter),重写可以添加一个setter

因此,这里要做的是重写,而不是重写属性,而是重写类的初始值设定项。下面是一个高度简化的示例:

class SomeChild: LiveCellTableViewController {
    override var sections: [SectionData] = {
        return [SectionData(title:  nil), SectionData(title: "Mutual"), SectionData(title: "Connections")]
    }()
}

去掉等号:

class C {
    var sections : [Int] = [1,2,3]
}

class CC : C {
    override init() {
        super.init()
        super.sections = [4,5,6]
    }
}

C().sections // [1,2,3]
CC().sections // [4,5,6]
这就是为什么Xcode需要括号。使用等号,代码块定义了一个内联函数,
()
调用该函数,使用结果初始化变量。如果没有
()
,您将尝试将函数分配给数组


如果没有
=
,您将使用getter定义一个属性,这(我假设)是您想要的。

您无法覆盖swift中存储的属性。您可以使用专用存储和计算属性来解决此问题。大概是这样的:

var sections: [SectionData] {
  return [SectionData(title: "aaa"), SectionData(title: "bbb"), SectionData(title: "ccc")]
}
另外,因为在您的示例中,您只是重写父类功能,并且您的属性是不可变的,所以您可以在init期间将其作为传入的参数

 //: Playground - noun: a place where people can play

import Cocoa

struct SectionData {
    let title: String
}

class ParentClass {

    private let defaultSections = [SectionData(title: "Parent")]

    var sections: [SectionData] {
        return defaultSections
    }

}

class ChildClass: ParentClass {

    private let childSections = [SectionData(title: "Child")]

    override var sections: [SectionData] {
        return childSections
    }

}

// If you wanted a child who appends sections

class AnotherChildClass: ParentClass {

    private let childSections = [SectionData(title: "Child")]

    override var sections: [SectionData] {
        return super.sections + childSections
    }

}

创建子类后,只需更改子类中的值,如果从情节提要加载,最简单的地方是
awakeFromNib

//: Playground - noun: a place where people can play

import Cocoa

struct SectionData {
    let title: String
}

class ParentClass {

    let sections: [SectionData]

    init(sections: [SectionData]) {
        self.sections = sections
    }
}

class ChildClass: ParentClass {

}

let parent = ParentClass(sections: [SectionData(title: "Parent")])
let child = ChildClass(sections: [SectionData(title: "Child")])

你有什么错误?另外,我不清楚为什么在
}
之后有
()
。除非它添加它,否则XCode不会生成(它也建议这样做)@因为
()
实际上运行块。如果省略赋值运算符(使属性成为计算属性),则应省略
()
。是的,这就是为什么我认为@TIMEX意味着要执行计算属性。@是的,我注意到了下面的答案。这与原始代码有很大不同,也与表面上的意图不同。原始属性创建一个存储属性,并使用块的返回值对其进行初始化,以便以后可以对其进行更改。您的将创建一个计算属性,该属性将始终返回相同的值,并且无法更改。在我看来,计算属性就是我们想要的。
class SomeChild: LiveCellTableViewController {
    override func awakeFromNib() {
        super.awakeFromNib()

        sections = [SectionData(title:  nil), SectionData(title: "Mutual"), SectionData(title: "Connections")]
    }
}