Swift - Type has no member

2019-06-07 07:27发布

I have a basic class named APIGroup that exists for the sole purpose of subclassing (it is not [yet] used for anything but certain static properties that are accessed by the subclasses). It looks somewhat like this:

public class APIGroup {
    public static let someProperty : String = "I am a property!"
}

And I have a subclass, Track, which defines a type method search as follows:

public class Track : APIGroup {
    public static func search(name: String) -> Void {
        print("Track search initiated. Name: \(name)")
        print("And the property is: \(someProperty)")
    }
}

These two classes are defined in separate files in the same module. In another target, I import the module and try to call:

import MyModule

MyModule.Track.search("testing...")

But this throws: Type 'Track' has no member 'search'. What am I doing wrong?

1条回答
做个烂人
2楼-- · 2019-06-07 08:19

Putting this code in a Playground works fine for me:

import Foundation

public class APIGroup {
    public static let someProperty : String = "I am a property!"
}

public class Track : APIGroup {
    public static func search(name: String) -> Void {
        print("Track search initiated. Name: \(name)")
        print("And the property is: \(someProperty)")
    }
}

Track.search("testing...")

If they are in the same module you do not need to import or use the module name.

enter image description here

查看更多
登录 后发表回答