Swift 3:NSCoder崩溃(encodeWithCoder:发送到实例的无法识别的选择器)

时间:2017-06-09 20:19:04

标签: ios swift crash nskeyedarchiver nscoder

单击保存按钮时崩溃我的应用程序的错误

  

- [BlogApp.Blog encodeWithCoder:]:无法识别的选择器发送到实例0x7fbe31d33800

     

***由于未捕获的异常终止应用' NSInvalidArgumentException',原因:'' - [BlogApp.Blog   encodeWithCoder:]:发送到实例的无法识别的选择器   0x7fbe31d33800'

MainController.swift中的代码声明'路径'

let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]

应该保存数组的按钮中的代码

// After Updating Table, Save Arrays
var success = false

success = NSKeyedArchiver.archiveRootObject(gameArray, toFile: path)

if success {
  print("Saved Blogs")
} else  {
  print("Didn't Save Blogs")
}

处理NSCoding和我的博客对象的代码

import UIKit

class BlogsCoding: NSObject, NSCoding {

var blogList : [Blog]

init(blogList : [Blog]) {
    self.blogList = blogList
}

convenience required init?(coder aDecoder: NSCoder) {

    guard let blogList = aDecoder.decodeObject(forKey: "blogs") as? [Blog]
        else {
            return nil
    }
    self.init (blogList : blogList)
}

func encode(with aCoder: NSCoder) {

    aCoder.encode(blogList, forKey: "blogs")
 }
}

class Blog: NSObject, NSCoding { // To conform to NSCoding

// Strings
var blogName: String?
var blogStatus1: String?
var blogStatus2: String?
var blogURL: String?
var blogID: String?
var blogType: String?
var blogDate: String?
var blogPop: String?
var blogList : [Blog] // To conform to NSCoding

override init() {

}
// Converting Strings into Objects
init(blogName bName: String,
     andBlogStatus1 bStatus1: String,
     andBlogStatus2 bStatus2: String,
     andBlogURL bURL: String,
     andBlogID bID: String,
     andBlogType bType: String,
     andBlogDate bDate: String,
     andBlogPop bPop: String,
     blogList : [Blog]) // To conform to NSCoding
{
    super.init()

    self.blogName = bName
    self.blogStatus1 = bStatus1
    self.blogStatus2 = bStatus2
    self.blogURL = bURL
    self.blogID = bID
    self.blogType = bType
    self.blogDate = bDate
    self.blogPop = bPop
    self.blogList = blogList // To conform to NSCoding
 }

 // To conform to NSCoding
 convenience required init?(coder aDecoder: NSCoder) {

    guard let blogList = aDecoder.decodeObject(forKey: "blogs") as? [Blog]
        else {
            return nil
    }
    self.init (coder : blogList) // *New error is here*
}

func encode(with aCoder: NSCoder) {

    aCoder.encode(blogList, forKey: "blogs")
}

}

在MainController.swift中

// Retrieving Data from Server
func retrieveData() {

    let getDataURL = "http://blogtest.com/receiving.php"
    let url: NSURL = NSURL(string: getDataURL)!

    do {

        let data: Data = try Data(contentsOf: url as URL)
        jsonArray = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as! NSMutableArray

        // Looping through jsonArray
        for i in 0..<jsonArray.count {

            // Create Blog Object
            let bID: String = (jsonArray[i] as AnyObject).object(forKey: "id") as! String
            let bName: String = (jsonArray[i] as AnyObject).object(forKey: "blogName") as! String
            let bStatus1: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus1") as! String
            let bStatus2: String = (jsonArray[i] as AnyObject).object(forKey: "blogStatus2") as! String
            let bURL: String = (jsonArray[i] as AnyObject).object(forKey: "blogURL") as! String
            // New
            let bType: String = (jsonArray[i] as AnyObject).object(forKey: "blogType") as! String
            let bDate: String = (jsonArray[i] as AnyObject).object(forKey: "blogDate") as! String
            let bPop: String = (jsonArray[i] as AnyObject).object(forKey: "blogPop") as! String
            // NSCoding
            let bList: NSObject = ((jsonArray[i]) as! NSObject).value(forKey: "blogList") as! NSObject

            // Add Blog Objects to Blog Array
            blogArray.append(Blog(gameName: bName, andBlogStatus1: bStatus1, andBlogStatus2: bStatus2, andBlogURL: bURL, andBlogID: bID, andBlogType: bType, andBlogDate: bDate, andBlogPop: bPop, blogList: bList as! [Game]))

        }
    }
    catch {
        print("Error: (Retrieving Data)")
    }

    myTableView.reloadData()
}

我显然做错了,因为它崩溃了,但是我正在按照教程进行操作,而我似乎无法弄清楚我做错了什么,我的猜测是我怎么做的。救了吗?或者&#39;路径&#39;出错了?它应该保存到一个正确的plist?谢谢!

  • 将Blog:NSObject符合NSCoding
  • 时出错
  

无法转换类型的值&#39; [博客]&#39;预期参数类型&#39; NSCoder&#39;

0 个答案:

没有答案