我正在用解析实现模型。
这是我的代码。
import Foundation
import UIKit
import Parse
class User {
var objectId : String
var username : String
var nickName : String
var phoneNumber : String
var preferPhoneNumber : String
var emailAddress : String
var preferemailAddress : String
var emailVerified : Bool
var createdAt : NSDate
var updatedAt : NSDate
var profile_picture : UIImage
init(objectId:String, username:String, nickName:String, phoneNumber: String, preferPhoneNumber : String, emailAddress : String, preferemailAddress : String, emailVerified : Bool){
self.objectId = (PFUser.currentUser()?.objectId)!
self.username = (PFUser.currentUser()?.username)!
if var nickName = (PFUser.currentUser()?.objectForKey("nickName") as? String){
(PFUser.currentUser()?.objectForKey("nickName") as? String)
}else{
"!NoData"
}
if var phoneNumber = (PFUser.currentUser()?.objectForKey("phoneNumber") as? String){
self.phoneNumber = (PFUser.currentUser()?.objectForKey("phoneNumber") as? String)!
}else{
"!NoData"
}
if var preferPhoneNumber = (PFUser.currentUser()?.objectForKey("preferPhoneNumber") as? String){
self.preferPhoneNumber = (PFUser.currentUser()?.objectForKey("preferPhoneNumber") as? String)!
}else{
"!NoData"
}
if var emailAddress = (PFUser.currentUser()?.objectForKey("emailAddress") as? String){
self.emailAddress = (PFUser.currentUser()?.objectForKey("emailAddress") as? String)!
}else{
"!NoData"
}
if var preferemailAddress = (PFUser.currentUser()?.objectForKey("preferemailAddress") as? String){
self.preferemailAddress = (PFUser.currentUser()?.objectForKey("preferemailAddress") as? String)!
}else{
"!NoData"
}
if var emailVerified = (PFUser.currentUser()?.objectForKey("emailVerified") as? Bool){
self.emailVerified = (PFUser.currentUser()?.objectForKey("emailVerified") as? Bool)!
}else{
"!NoData"
}
}
}
在我的代码中,我在制作模型时收到“从初始化程序返回而没有初始化所有存储属性”的错误。
我试着把“返回零”放在最后,这不是解决方案。
所以我想知道如何初始化存储的属性?
或如何解决此错误?
答案 0 :(得分:1)
初始化程序完成后,所有存储的属性都需要具有值。快速浏览后,我发现至少有3个变量你没有赋值给:
var createdAt : NSDate
var updatedAt : NSDate
var profile_picture : UIImage
此外,我认为您的if分支也会与该概念发生冲突,因为在其他情况下,您再次错过了分配值。
要么提供所有这些字段的默认值或实际值,要么将它们更改为选项,例如NSDate?
或NSDate!
,String?
或String!
等。< / p>