ios - 类型的值没有成员
<p><p>我已经学习了一段时间的快速类(class),并跟随在线 tuts。大多数问题我都能找到,但我可以在这个问题上使用一些帮助。</p>
<p>在一个应用中,我们正在处理用户通过 Firebase FIRAuth 登录时可能发生的错误,我的代码如下所示:</p>
<pre><code>class AuthService {
private static let _instance = AuthService()
static var instance: AuthService {
return _instance
}
func login(email: String, password: String, onComplete: Completion?) {
FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: {(user, error) in
if error != nil {
if let errorCode = FIRAuthErrorCode(rawValue: error!.code){
if errorCode == .errorCodeUserNotFound {
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: {(user, error) in
if error != nil {
self.handleFirebaseError(error: error!, onComplete: onComplete)
} else {
if user?.uid != nil {
//Sign in
FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: {(user, error)
in
if error != nil {
self.handleFirebaseError(error: error!, onComplete: onComplete)
} else {
onComplete?(errMsg: nil, data: user)
}
})
}
}
})
}
} else {
self.handleFirebaseError(error: error!, onComplete: onComplete)
}
} else {
onComplete?(errMsg: nil, data: user)
}
})
}
func handleFirebaseError(error: NSError, onComplete: Completion?) {
print(error.debugDescription)
if let errorCode = FIRAuthErrorCode(rawValue: error.code) {
switch(errorCode) {
case .errorCodeInvalidEmail:
onComplete?(errMsg: "Invalid email adress", data: nil)
break
case .errorCodeWrongPassword:
onComplete?(errMsg: "Invalid password", data: nil)
break
case .errorCodeEmailAlreadyInUse, .errorCodeAccountExistsWithDifferentCredential:
onComplete?(errMsg: "Email already in use", data: nil)
break
default:
onComplete?(errMsg: "There was a problem authenticating, try again", data: nil)
break
}
}
</code></pre>
<p>在编译时,我在第一个函数中遇到错误:</p>
<pre><code>if let errorCode = FIRAuthErrorCode(rawValue: error!.code)
</code></pre>
<p>说“'错误'类型的值没有成员'代码'”。第二个函数使用完全相同的代码行,但没有错误。我尝试了各种方法,例如打开包装或不打开包装。</p>
<p>例如,添加一个 0 可以让代码编译,但一旦出现错误就会中断。</p>
<p>提前感谢您的宝贵时间! </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果我没看错,你是在问这两行代码有什么区别(你将其描述为<em>“完全相同”</em>:</p>
<pre><code>if let errorCode = FIRAuthErrorCode(rawValue: error!.code){
if let errorCode = FIRAuthErrorCode(rawValue: error.code) {
</code></pre>
<p>当打印在一起时,差异变得明显。在第一行中,<code>error!</code> 表示错误在该点永远不能为零。因此,如果是,您会收到运行时错误。在第二行中,<code>error</code> 可以为 nil,如果是,那么 let 语句将通过 <code>else</code> 或下一个适用的语句。</p>
<p>一般来说,避免使用 !在你的代码中强制使用非零值,除非你 101% 确定它总是如此。</p></p>
<p style="font-size: 20px;">关于ios - 类型的值没有成员,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38990908/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38990908/
</a>
</p>
页:
[1]