我一直在学习 swift 类(class)并跟随在线 tuts。大多数问题我都能找到,但我可以在这个问题上寻求帮助。
在一个应用程序中,我们正在处理用户通过 Firebase FIRAuth 登录时可能发生的错误,我的代码如下所示:
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
}
}
在编译时,我在第一个函数中遇到错误:
if let errorCode = FIRAuthErrorCode(rawValue: error!.code)
说“‘错误’类型的值没有成员‘代码’”。第二个函数使用完全相同的代码行但没有错误。我尝试了各种各样的事情,比如解包或不解包,但都没有成功。
例如,添加一个 0 可以让代码编译,但一旦出现错误就会中断。
提前感谢您的宝贵时间!
最佳答案
如果我没看错的话,你是在问这两行代码有什么区别(你描述为“完全一样”:
if let errorCode = FIRAuthErrorCode(rawValue: error!.code) {
if let errorCode = FIRAuthErrorCode(rawValue: error.code) {
当一起打印时,差异变得很明显。在第一行中,error! 表示此时 error 绝不能为 nil。因此,如果是,您会收到运行时错误。在第二行中,error 可以为 nil,如果是,则 let 语句将落入 else 或适用的下一个后续语句。
一般来说,避免使用 !在你的代码中强制使用非 nil 值,除非你 101% 确定它总是如此。
关于ios - 类型的值没有成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38990908/
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳