FH- FH- - 3 months ago 7
Objective-C Question

How to use Cloudmine as API in iOS Swift

Calling APIs via Cloudmine is very different on how to consume a REST or SOAP. Now I am having trouble in the syntax, the documentation in cloudmine website is in objective C, when I translate it to Swift, it gives me error. Here's my sample code for the function log in:

user!.loginWithCallback({(resultCode: CMUserAccountResult!, messages: [AnyObject!]) -> Void in

switch resultCode {
case CMUserAccountLoginSucceeded:
// success! the user now has a session token
var token = user.token
break

case CMUserAccountLoginFailedIncorrectCredentials: break
// the users credentials were invalid

case CMUserAccountOperationFailedUnknownAccount: break
// this account doesn't exist

}

})


I just converted it from objective c code which is:

[user loginWithCallback:^(CMUserAccountResult resultCode, NSArray *messages) {
switch(resultCode) {
case CMUserAccountLoginSucceeded:
{ // success! the user now has a session token
NSString *token = user.token;
break;
}
case CMUserAccountLoginFailedIncorrectCredentials:
// the users credentials were invalid
break;
case CMUserAccountOperationFailedUnknownAccount:
// this account doesn't exist
break;
}
}];


Now it gives me error:


Cannot convert value of type '(CMUserAccountResult!, [AnyObject!]) ->
Void'
to expected argument type 'CMUserOperationCallback!'

FH- FH-
Answer

This is quite embarrassing, I just looked at what the init of message, and it turns out it must be [AnyObject]!

So I just changed the code to:

user!.loginWithCallback({(resultCode: CMUserAccountResult, messages: [AnyObject]!) -> Void in

    switch resultCode {
    case CMUserAccountLoginSucceeded:
        // success! the user now has a session token
        var token = user.token
        break

    case CMUserAccountLoginFailedIncorrectCredentials: break
        // the users credentials were invalid

    case CMUserAccountOperationFailedUnknownAccount: break
        // this account doesn't exist

    }

})