ios - 实例成员不能用于类 NSObject 的类型

标签 ios swift swift2

我收到一个显示以下消息的错误:

logoImage cannot be used on type 'Home'

我很困惑为什么会收到此错误消息。任何帮助是极大的赞赏!

如果您需要更多信息来帮助回答问题,请发表评论,我会为您提供所需的信息。

提前致谢!

class Home: NSObject {

    var logoImage: [String] = [
         "apple.png",
         "Mango.png",
         "Strawberry.png",
         "Cat.png",
        ]

    class func homeDisplayImage() -> String  {
        for var i = 0; i < 3; i++ {
            return logoImage[i]
        }
    }
}

最佳答案

在 Java 意义上; static 函数不能引用非static 成员。在 Swift 中也是如此。您的 logoImage 是非 statichomeDisplayImage 是一个 static 函数。

您需要从函数中删除 class:

func homeDisplayImage() -> String  {
for var i = 0; i < 3; i++ {
    return logoImage[i]
}

或者添加一个static到你的var:

static var logoImage: [String] = [
    "apple.png",
    "Mango.png",
    "Strawberry.png",
    "Cat.png",
]

关于ios - 实例成员不能用于类 NSObject 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35617586/

相关文章:

ios - Mapkit 标注附件按钮激活

swift - Deinit 方法从未被调用 - Swift Playground

arrays - 在 Swift3 中对数组进行排序

ios - 如何在点击时更新静态 UITableView 中的 UILabel?

ios - swift 2 : Can't find audio file path when I use variables as file names

ios - App Store Connect消息:您的帐户很快将需要迁移到联合身份验证

ios - Swift2- NSTimer崩溃

ios - 找不到匹配的配置文件(有效的配置文件均不允许指定的权利)

ios - 在 iOS 9.3/Xcode 7.3 中使用 StoreKit 常量时使用未解析的标识符

android - 适用于 iOS 和 Android 应用程序的服务器端数据库