Timmy T Timmy T - 1 month ago 15
iOS Question

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

I haven't been able to solve this problem on Xcode.

Here is how the Constants are defined

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"

}


The main problem is on this line

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


The error that I receive when I try to build and/or run a code analysis gives me the following compiler error.


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


Here is the code from the provider declaration all the way down to the end.

// 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)
}

}


What does this mean?

Answer

The error message Instance member 'CognitoPoolID' cannot be used on type 'Constants' means that you are applying CognitoPoolID to the name of a class or struct, Constants, but it is an instance property. If you meant this to be a class or static property, you needed to declare it with class or static.

So, in your case, you have said:

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

You should have said:

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

Just make that change. Now your code will compile (and work) correctly.