Swift - Type has no member

試著忘記壹切 提交于 2020-01-03 03:08:06

问题


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:


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.



来源:https://stackoverflow.com/questions/37355179/swift-type-has-no-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!