Swift-类型没有成员 [英] Swift - Type has no member

查看:137
本文介绍了Swift-类型没有成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 APIGroup 的基本类,该类仅出于子类化的目的而存在([尚不用于]子类访问的某些静态属性)。看起来有点像这样:

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!"
}

我有一个子类, Track ,它定义了 type 方法 search ,如下所示:

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?

推荐答案

将此代码放在Playground中对我来说很好:

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.

这篇关于Swift-类型没有成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆