问题描述
在aws.amazon.com/mobile/的当前示例代码之后,有许多代码项没有很好地更新到Swift 3(考虑到它们的资源,这很奇怪).
Following along the current sample code for aws.amazon.com/mobile/ there are a number of code items they have not updated really well to Swift 3 (which is pretty bizarre considering their resources).
当您访问AWSMobileClient.swift时,有一行这样的代码:
When you come to AWSMobileClient.swift there's a line of code like this:
if (!isInitialized) {
AWSIdentityManager.defaultIdentityManager().resumeSession(completionHandler:
{(result: AnyObject?, error: NSError?) -> Void in
print("Result: \(result) \n Error:\(error)")
} as! (Any?, Error?) -> Void)
isInitialized = true
}
任何运行都会崩溃.
幸运的是,我能够通过更改参数来修复它
Fortunately I was able to fix it by changing the arguments like so
AWSIdentityManager.defaultIdentityManager().resumeSession(completionHandler:
{(result: Any?, error: Error?) -> Void in
print("Result: \(result) \n Error:\(error)")
} as! (Any?, Error?) -> Void)
问题是这样,我得到警告
The problem is this, I get the warning
实际上,将其强制转换为相同类型似乎毫无意义.
indeed it seems pointless trying to cast it to the same type.
但是,如果您只是删除演员表
AWSIdentityManager.defaultIdentityManager().resumeSession(completionHandler:
{(result: AnyObject?, error: NSError?) -> Void in
print("Result: \(result) \n Error:\(error)")
}
isInitialized = true
那根本不起作用,您会遇到各种奇怪的语法错误
that just doesn't work at all, you get all sorts of weird syntax errors
1)为什么那里仍然有演员?
1) Why is there a cast there anyway?
2)我如何摆脱演员表,或正确书写它,以确保没有相同的演员表,因此也没有警告?
2) How can I get rid of the cast, or write it properly so there's no identical cast and hence no warning?
3)我猜想解决方案只是消除作用力(因此,as
而不是as!
),但我真的不明白为什么必须在此处施放砌块.
3) I guess a solution is just to remove the force (so, as
rather than as!
) but I really don't understand why you have to cast the block there.
注意-在同一行代码中,Amazon包含一条注释如果您在iOS Simulator中获得EXC_BAD_ACCESS,请执行Simulator->重置内容和设置...,这将清除其他具有相同束ID的应用程序存储的错误身份验证令牌." 我在这里询问的问题与之完全无关.
Note - at that same line of code, Amazon include a comment "If you get an EXC_BAD_ACCESS here in iOS Simulator, then do Simulator -> Reset Content and Settings..., This will clear bad auth tokens stored by other apps with the same bundle ID." The issue I'm asking about here is totally unrelated to that.
推荐答案
在最后一个可能是正确语法的示例中,缺少右括号.
In the last example which is presumably the proper syntax the closing parenthesis is missing.
您替换了
} as (Any?, Error?) -> Void)
与
}
而不是预期的})
我删除了所有不明确的内容:
I deleted everything which is not mandatory to make it clearer:
WSIdentityManager.defaultIdentityManager().resumeSession(completionHandler: { (result, error) in
print("Result: \(result) \n Error:\(error)")
})
isInitialized = true
或使用结尾闭包语法更短:
or still shorter with trailing closure syntax:
WSIdentityManager.defaultIdentityManager().resumeSession() { (result, error) in
print("Result: \(result) \n Error:\(error)")
}
isInitialized = true
不需要类型注释(在完成块中).他们弊大于利.
The type annotations (in completion blocks) are not needed. They could do more harm than good.
PS:isInitialized
是否应该位于完成区中?
PS: Isn't isInitialized
supposed to be in the completion block?
这篇关于最新Amazon AWS Mobile示例代码的怪异Swift 3问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!