Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array extension called from other module

Tags:

swift

Array extension methods are unavailable from other modules (for example the XCTest project)

For the sake of 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's here only to show the problem
        return 0
    }
}

Calling it from the same module works as expected but from a test class don't

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

I think this is correct because the method visibility is restricted to its own module, indeed I obtain the error '[String]' does not have a member named 'myMethod'

I've tried to define the extended method as public as shown below

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

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

Until Beta 7 using public solved the problem but under XCode 6.1 (6A1046a) I obtain this error

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

like image 473
dafi Avatar asked Sep 30 '22 15:09

dafi


1 Answers

Swift does not allow public extensions currently so you will need to include that extension swift file in your project and put it part of the target.

like image 134
Encore PTL Avatar answered Oct 06 '22 20:10

Encore PTL