gpt4 book ai didi

ios - 显示错误不可失败初始化程序 "init(coder:)"错误

转载 作者:行者123 更新时间:2023-11-29 01:45:41 28 4
gpt4 key购买 nike

当我尝试运行 int(coder:) 时它显示此错误,我不知道为什么? “不可失败的初始化器要求 init(coder:) 不能被可失败的初始化器 ('init?') 满足”

class Note: NSObject, NSCoding {

var name: String
var photo: UIImage?
var rating: Int

static let DocumentsDirectory: AnyObject = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("notes")

struct PropertyKey {
static let nameKey = "name"
static let photoKey = "photo"
static let ratingKey = "rating"
}


init?(name: String, photo: UIImage?, rating: Int) {

self.name = name
self.photo = photo
self.rating = rating

super.init()

// Initialization should fail if there is no name or if the rating is negative.
if name.isEmpty || rating < 0 {
return nil
}

}

func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: PropertyKey.nameKey)
aCoder.encodeObject(photo, forKey: PropertyKey.photoKey)
aCoder.encodeInteger(rating, forKey: PropertyKey.ratingKey)
}

required convenience init?(coder aDecoder: NSCoder) {

let name = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String

// Because photo is an optional property of Meal, use conditional cast.
let photo = aDecoder.decodeObjectForKey(PropertyKey.photoKey) as? UIImage

let rating = aDecoder.decodeIntegerForKey(PropertyKey.ratingKey)

// Must call designated initializer.
self.init(name: name, photo: photo, rating: rating)

}


}

我正在使用 xcode 6,顺便说一句,当这段代码在 xcode 7 中运行时,它没有显示任何错误,原因是什么?

最佳答案

所以我找到了一种让它工作的方法,“init(coder:)”方法不能失败,所以我所做的就是重写“init(coder:)”调用的“init()”方法)' 方法,因为它需要调用 'self.init()'。所以这是代码:

class Meal: NSObject, NSCoding {

// MARK: Archiving Paths
static let DocumentsDirectory = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("meals")

// MARK: Properties
var name: String
var rating: Int
var photo: UIImage?

// MARK: Types
struct PropertyKey {
static let nameKey = "name"
static let photoKey = "photo"
static let ratingKey = "rating"
}

// MARK: Initialization

init? (name: String, rating: Int, photo: UIImage?) {
// Intialize stored properties.
self.name = name
self.rating = rating
self.photo = photo

super.init()

// Initialization should fail if there is no name or if the rating is negative.
if self.name.isEmpty || (rating < 0) {
return nil
}
}

override private init () {
self.name = ""
self.rating = 0
self.photo = nil
}

// MARK: NSCoding

func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.name, forKey: PropertyKey.nameKey)
aCoder.encodeObject(self.photo, forKey: PropertyKey.photoKey)
aCoder.encodeObject(self.rating, forKey: PropertyKey.ratingKey)
}

convenience required init(coder aDecoder: NSCoder) {
self.init()

let name = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String
if let rating = aDecoder.decodeIntegerForKey(PropertyKey.ratingKey) {
self.rating = rating
}

// Because photo is an optional property of Meal, use conditional cast.
let photo = aDecoder.decodeObjectForKey(PropertyKey.photoKey) as? UIImage

self.name = name
self.photo = photo
}
}

我将“init()”方法设为私有(private),这样只有类内部的方法才能调用它。我还必须选择解包评级,因为应用程序崩溃并提示无法使用 ratingKey 取消存档 Int。

关于ios - 显示错误不可失败初始化程序 "init(coder:)"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31955898/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com