swift - 如何避免子类中默认初始化参数冗余?

标签 swift generics default-value

我想为某些初始化参数提供默认值。我希望能够在子类中重用相同的默认值,但没有找到方法来做到这一点。

第一次尝试 - 参数默认值:

class A { 

    typealias Mapper = (A) -> String

    let mapper: Mapper

    init(mapper: Mapper = {a in "foo"}) {
        self.mapper = mapper
    }

}

class B: A {

    let myVar: Int

    init(myVar: Int, mapper: Mapper = {a in "foo"}) {
        self.myVar = myVar

    }
}

let b: B = B(myVar: 1)
let str = b.mapper(b)
let b2: B = B(myVar: 2, mapper: {a in "bar"})

我必须指定默认值两次(在 A 和 B 初始化中)才能使用或不使用默认值来初始化 B。

我也尝试过使用便利初始化程序,同样的问题:

class A {

    typealias Mapper = (A) -> String

    let mapper: Mapper

    convenience init() {
        self.init(mapper: {a in "foo"})
    }

    init(mapper: Mapper) {
        self.mapper = mapper
    }

}

class B: A {

    let myVar: Int

    convenience init(myVar: Int) {
        self.init(myVar: myVar, mapper: {a in "foo"})
    }

    init(myVar: Int, mapper: Mapper) {
        self.myVar = myVar

        super.init(mapper: mapper)
    }
}

我想也许至少我将默认值放在静态变量中,如下所示:

class A {

    typealias Mapper = (A) -> String

    static let defMapper: Mapper = {a in "foo"}

    let mapper: Mapper

    convenience init() {
        self.init(mapper: A.defMapper)
    }

    init(mapper: Mapper) {
        self.mapper = mapper
    }

}

class B: A {

    let myVar: Int

    convenience init(myVar: Int) {
        self.init(myVar: myVar, mapper: A.defMapper)
    }

    init(myVar: Int, mapper: Mapper) {
        self.myVar = myVar

        super.init(mapper: mapper)
    }
}

(也可以将变量定义为惰性变量,以避免在不使用默认参数时初始化)。

这一直有效,直到我向 A 添加泛型类型为止。然后我得到“泛型类型尚不支持静态属性”。所以我必须将默认值放在类之外,这导致也必须将类型别名定义放在类之外,如果我将泛型添加到混合中,这将是一团糟。

有办法解决这个问题吗?

最佳答案

如何使用计算属性:

class A<T> {

typealias Mapper = (A) -> String

static var defMapper: Mapper {
    return { a in "foo" }
}

let mapper: Mapper

convenience init() {
    self.init(mapper: A.defMapper)
}

init(mapper: Mapper) {
    self.mapper = mapper
}

}

class B<T>: A<T> {

    let myVar: Int

    convenience init(myVar: Int) {
        self.init(myVar: myVar, mapper: A.defMapper)
    }

    init(myVar: Int, mapper: Mapper) {
        self.myVar = myVar

        super.init(mapper: mapper)
    }
}

let b: B = B<String>(myVar: 1)

let str = b.mapper(b)

let b2: B = B<String>(myVar: 2, mapper: {a in "bar"})

A<Int>(mapper: {a in "bar"})

A<Int>()

或类型方法:

class A<T> {

    typealias Mapper = (A) -> String

    static func defMapper() -> Mapper {
        return { a in "foo" }
    }

    let mapper: Mapper

    convenience init() {
        self.init(mapper: A.defMapper())
    }

    init(mapper: Mapper) {
        self.mapper = mapper
    }

}

class B<T>: A<T> {

    let myVar: Int

    convenience init(myVar: Int) {
        self.init(myVar: myVar, mapper: A.defMapper())
    }

    init(myVar: Int, mapper: Mapper) {
        self.myVar = myVar

        super.init(mapper: mapper)
    }
}

let b: B = B<String>(myVar: 1)

let str = b.mapper(b)

let b2: B = B<String>(myVar: 2, mapper: {a in "bar"})

A<Int>(mapper: {a in "bar"})

A<Int>()

关于swift - 如何避免子类中默认初始化参数冗余?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30073785/

相关文章:

ios - 快速滚动时隐藏显示顶 View

ios - Firebase 数据库在删除节点时不断重新插入节点

java - 这个通用函数的作用是什么?

php - 你能在 Javascript 中声明兼函数参数吗?

c# - 在 C# 中设置对象的默认值 `String`

ios - 如何关闭 Sprite 套件节点纹理的抗锯齿

swift - 按需资源的 Assets 目录编译错误 : has no output specification

c# - try block 内的通用输出值赋值

具有 self 类型要求的协议(protocol)的 Swift 类型删除

java - 默认值发生了什么变化?