2015-05-06 46 views
2

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

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

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中INIT),以便能够有或没有默认值来初始化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通用类型然后我得到“静态属性尚未支持泛型类型”。所以我必须在类之外放置默认值,这也导致必须将typealias定义放在类之外,如果我将泛型添加到混合中,这会非常混乱。

有没有办法解决这个问题?

回答

2

如何使用计算性能:

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>() 
+0

啊,没有想到这一点。我以为我错过了与初始化程序相关的东西,所以我没有足够的探索“替代方法”。谢谢! – Ixx