【问题标题】:Cannot convert value of type 'String?' to expected argument type '_?'无法转换“字符串”类型的值?到预期的参数类型“_?”
【发布时间】:2019-12-24 00:23:59
【问题描述】:

我正在实现一个函数来检索自定义身份提供商的 IdentityID 和令牌,作为我在 Amazon Cognito 中设置身份验证工作的一部分。我需要将令牌添加为我的 return 语句的一部分,但在我的 return 语句中,我收到了一个错误。
“无法转换 'String 类型的值?'到预期的参数类型 '_?'"。

我不明白什么_?论据是准确的以及如何符合它。

import AWSCore
/*
 * Use the token method to communicate with your backend to get an
 * identityId and token.
 */
class DeveloperAuthenticatedIdentityProvider : AWSCognitoCredentialsProviderHelper {
    override func token() -> AWSTask<NSString> {
    //Write code to call your backend:
    //pass username/password to backend or some sort of token to authenticate user, if successful, 
    //from backend call getOpenIdTokenForDeveloperIdentity with logins map containing "your.provider.name":"enduser.username"
    //return the identity id and token to client
    //You can use AWSTaskCompletionSource to do this asynchronously

    // Set the identity id and return the token
    self.identityId = resultFromAbove.identityId
    return AWSTask(result: resultFromAbove.token)
}

resultFromAbove.token 是 String?。所以当我使用它时,我得到了错误。但是,如果我只是输入一个字符串,例如

return AWSTask(result: "abcd")

这似乎很好。我在这里错过了什么?

【问题讨论】:

    标签: swift arguments amazon-cognito


    【解决方案1】:

    改变

    返回 AWSTask(result: resultFromAbove.token)

    return AWSTask(result: resultFromAbove.token as NSString?)

    【讨论】:

    • 我收到以下错误。 '字符串?不能转换为“NSString”;你的意思是用'as!'强制沮丧?。作为!或作为?作品。如果我像 NSString? 那样做,那也可以,那么这里最好的方法是什么?几个额外的问题。我们是因为 AWSTask 期待 NSString 而强制转换为 NSString 吗?你知道吗,在我原来的错误中,参数类型“_”是什么?什么意思?
    • @Sherry 更正了答案,在我看来,最好的方法是 NSString?因此,如果结果为 nil,您将 return AWSTask(result: nil) 不会引发错误。我不知道什么参数类型'_?意思是,对不起。
    猜你喜欢
    • 2021-09-18
    • 2018-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多