Swift 3: 实例成员 'CognitoPoolID' 不能用于类型 'Constants'

Swift 3: Instance member 'CognitoPoolID' cannot be used on type 'Constants'

我没能在 Xcode 上解决这个问题。

常量的定义如下

struct Constants {

let AWSAccountID = "XXXXXXXXXXXX"
let CognitoPoolID = "arn:aws:XXXXXXX-XXXXXXXX:XX-XXXXXXXXX-X:XXXXXXXXXXXX:XXXXXXXXXXXX/XX-XXXXXXXX-X:XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
let CognitoRoleAuth = "arn:aws:iam::XXXXXXXXXXXX:XXXX/XXXXXXX_XXXXXXXX_XXXX" 

}

主要问题出在这一行

let provider = AWSCognitoCredentialsProvider(...
        identityPoolId:Constants.CognitoPoolID.value,...)

我在尝试构建 and/or 运行 时收到的错误代码分析给出了以下编译器错误。

Instance member 'CognitoPoolID' cannot be used on type 'Constants'

这是从提供商声明一直到最后的代码。

// This specifies AWS connection parameters.
    let provider = AWSCognitoCredentialsProvider (regionType: .apNortheast1,
                                                 identityPoolId: Constants.CognitoPoolID.value,
                                                 unauthRoleArn: "arn:aws:iam::XXXXXXXXXXXX:XXXX/XXXXXXX_XXXXXXXXXXX_XXXX",
                                                 authRoleArn: Constants.CognitoRoleAuth.value,
                                                 identityProviderManager: Constants.AWSAccountID.value);

    let configuration = AWSServiceConfiguration(region:.apNortheast1, credentialsProvider:provider);

    AWSServiceManager.default().defaultServiceConfiguration(configuration)

    // Set out the Auth0 URL here which is the same Open ID Connect Provider URL.
    provider.logins = ["marcadian.au.auth0.com" : idToken]

    provider.getIdentityId().continue({ (task:AWSTask!) -> Any? in

        self.provider.refresh()

        if (task.error != nil) {
            // Failed Error Handling code goes here.

        } else {
            // User has logged in successfully.

        }
        return nil
    })

    // User has dismissed the Lock View Controller.
    controller?.dismiss(animated: true, completion: nil)
  }

}

这是什么意思?

错误消息 Instance member 'CognitoPoolID' cannot be used on type 'Constants' 表示您正在将 CognitoPoolID 应用于 class 结构的名称Constants,但它是一个 实例 属性。如果您的意思是 class 或静态 属性,您需要使用 classstatic.

声明它

所以,在你的情况下,你说:

struct Constants {
    let AWSAccountID = // ...
    let CognitoPoolID = // ...
    let CognitoRoleAuth = // ...
}

你应该说:

struct Constants {
    static let AWSAccountID = // ...
    static let CognitoPoolID = // ...
    static let CognitoRoleAuth = // ...
}

只需进行更改即可。现在您的代码将正确编译(并工作)。