其他模块(例如 XCTest 项目)无法使用数组扩展方法
为了简单起见,下面的代码什么也不做,但可以用来重现错误
import Foundation
extension Array {
mutating func myMethod(toIndex: Int) -> Int! {
// no real code, it's here only to show the problem
return 0
}
}
从同一模块调用它可以按预期工作,但从测试类调用它则不然
class MyProjectTests: XCTestCase {
func testMoveObjectsFromIndexes1() {
var arr = ["000", "001", "002", "003"]
arr.myMethod(0)
}
}
我认为这是正确的,因为方法可见性仅限于其自己的模块,确实我收到了错误'[String]' does not have a member named 'myMethod'
我尝试将扩展方法定义为public
如下所示
extension Array {
public mutating func myMethod(toIndex: Int) -> Int! {
// no real code, it's here only to show the problem
return 0
}
}
但我收到编译错误'Extension of generic type 'Array<T>' from a different module cannot provide public declarations'
直到 Beta 7 使用public
解决了问题,但在 XCode 6.1 (6A1046a) 下我收到此错误
如何修复它以在其他模块/项目下运行?