0
是否有可能具有以下协议+扩展的方法,而不是一个类,以防止它被用作你可以实例化的类?如何使这个超类成为协议+扩展而不是类?
我遇到的问题是我无法使用自定义init来构建它。 Xcode不断抱怨说我使用self,但是调用super.init也不被接受。
class Entity: NSObject
{
var objectID: String?
var createdAt: NSDate?
var updatedAt: NSDate?
required override init()
{
}
required init(dictionary: Dictionary)
{
if let objectID = dictionary["objectId"] as? String
{
self.objectID = objectID
}
if let createdAt = dictionary["createdAt"] as? String
{
self.createdAt = NSDate.dateFromJSONDateString(createdAt)
}
if let updatedAt = dictionary["updatedAt"] as? String
{
self.updatedAt = NSDate.dateFromJSONDateString(updatedAt)
}
}
func dictionaryRepresentation() -> Dictionary
{
var dict = Dictionary()
if let objectID = objectID
{
dict["objectId"] = objectID
}
if let createdAt = createdAt
{
dict["createdAt"] = createdAt.JSONDateString()
}
if let updatedAt = updatedAt
{
dict["updatedAt"] = updatedAt.JSONDateString()
}
return dict
}
}
基本上我想实现的是符合实体的所有类有一个的objectID,createdAT和updatedAt并且可以用字典进行初始化,并得到了dictionaryRepresentation。