2016-04-22 52 views
3

我有以下擴展名:斯威夫特曖昧使用SUM()的

protocol Addable { 

    init() 

    func + (lhs: Self, rhs: Self) -> Self 

} 

extension Int: Addable {} 
extension Double: Addable {} 

extension SequenceType where Generator.Element: Addable { 

    func sum() -> Generator.Element { 

     return reduce(Generator.Element()) { $0 + $1 } 

    } 

} 

其中我嘗試在單元測試中使用:

func testThatArrayOfDoublesCanCalculateTheSumOfAllElements() { 

    let numbers = [1.0, 2.0, 3.0]   

    let myExpectedValue = 1.0 + 2.0 + 3.0 

    let myActualValue = numbers.sum() 

    XCTAssertEqual(myExpectedValue, myActualValue) 

} 

enter image description here

在Xcode中7.3編譯器給我一個模糊使用'sum()'。爲什麼?

側面板說:

enter image description here

+0

什麼是數字? –

+0

假設'數字'是一個'Int'或'Double'的數組,我無法再現這個編譯器錯誤。 – JAL

+0

更新了我的測試用例。 – weenzeel

回答

2

的問題是,我建設我的擴展文件都在我的框架,目標和我的測試目標。