Array extension called from another module

Array extension methods are not available from other modules (for example, the XCTest project)

For simplicity, the code below does nothing, but it can be used to reproduce the error.

import Foundation extension Array { mutating func myMethod(toIndex: Int) -> Int! { // no real code, it here only to show the problem return 0 } } 

A call from the same module works as expected, but from a test class it doesn’t

 class MyProjectTests: XCTestCase { func testMoveObjectsFromIndexes1() { var arr = ["000", "001", "002", "003"] arr.myMethod(0) } } 

I think this is correct, because the visibility of the method is limited by its own module, indeed I get the error '[String]' does not have a member named 'myMethod'

I tried to define the extended method as public , as shown below

 extension Array { public mutating func myMethod(toIndex: Int) -> Int! { // no real code, it here only to show the problem return 0 } } 

But I get a compilation error 'Extension of generic type 'Array<T>' from a different module cannot provide public declarations'

So far, beta 7 using public not solved the problem, but under Xcode 6.1 (6A1046a), I get this error

How can I fix it to work under other modules / projects?

+5
source share
2 answers

Swift does not currently allow public extensions, so you will need to include this quick extension file in your project and put it in the target.

+3
source

Not completely resolving the original question, I found that I could test extension methods in Swift 2.0 (under Xcode 7.0) by importing a module with the @testable directive:

@testable import MyGreatModule

+1
source

Source: https://habr.com/ru/post/1204751/


All Articles