私はこのクラスがNSCodingプロトコルに準拠していないというエラーが表示される理由を理解できません。たぶんもう一組の目が助けになるでしょう。私は各機能が何をしているのかを明確にするためにコメントを追加しようとしました。NSCodingに準拠
import Foundation
import UIKit
class Search: NSObject, NSCoding {
// Global Variables
var articlePicture: UIImage!
var articleTitle: String
// MARK: Public Init
init(articlePicture: UIImage, articleTitle: String) {
self.articlePicture = articlePicture
self.articleTitle = articleTitle
}
// Required NSCoding Protocol
required convenience init?(coder decoder: NSCoder) {
guard let articlePicture = decoder.decodeObject(forKey: "articlePicture") as? UIImage,
let articleTitle = decoder.decodeObject(forKey: "articleTitle") as? String
else { return nil }
self.init(
articlePicture: articlePicture,
articleTitle: articleTitle
)
}
// Required NSCoding Protocal
func encodeWithCoder(coder: NSCoder) {
coder.encode(self.articlePicture, forKey: "articlePicture")
coder.encode(self.articleTitle, forKey: "articleTitle")
}
// Initialize [Search]
static var searches = [Search]()
static func getSearches() -> [Search]{
return searches
}
static func setSearch(search: Search){
searches.append(search)
}
// NSUserDefaults Searches Array funcs
static func saveSearches(){
let searchData = NSKeyedArchiver.archivedData(withRootObject: searches)
UserDefaults.standard.set(searchData, forKey: "searches")
}
static func loadSearches() -> [Search]{
let searchData = UserDefaults.standard.object(forKey: "searches") as? NSData
if let searchData = searchData {
searches = (NSKeyedUnarchiver.unarchiveObject(with: searchData as Data) as? [Search])!
return searches
}
return searches
}
}
をお読みください。 NSCodingは完全に消えません。あなたはまだそれが必要です。あなたはその質問に全く答えていません。 – matt
Swift 4でデコード/エンコードするためにNSCodingは必要ありません。詳細な情報のリンクを読んだら理解できます。確かにその質問に対する答えではありません。私はそれをデコード/エンコードする別の方法を提供しました。これはもっと最新のものです。 –
あなたの最初の文が間違っています。 NSCodingは完全に変更されていません。それは全く変わっていない。そしてあなたはまだ 'encode(with)'を実装する必要があり、何をすべきか説明していません。最後に、あなたの答えは尋ねられたものとは無関係です。リンク専用です。それは間違っている間違った答えです。 – matt