将自己的类的数组存储和读取到plist中?

时间:2015-05-21 12:30:56

标签: ios swift

我尝试将自己的类列表存储到plist中。为此,我也发现了一个类似的问题已经回答,但我仍然没有成功。

我的代码:

我的班级:

class Bookmark: NSObject {

    var sName:String="";
    var sTextPosition:String="";  // beim Text scrollpos

    init (coder aDecoder: NSCoder!) {
        self.sName = aDecoder.decodeObjectForKey("name") as! String;
        self.sTextPosition = aDecoder.decodeObjectForKey("textposition")as! String;
    }

    func encodeWithCoder(aCoder: NSCoder!) {
        aCoder.encodeObject(sName, forKey:"name");
        aCoder.encodeObject(sTextPosition, forKey:"textposition");
    }

}

我的代码用于存储书签数组:

 var bookmark1:Bookmark=Bookmark(); // <--"Missing argument for parameter 'coder' in call".
        bookmark1.sName="myname";
        bookmark1.sTextPosition="100";

        let listofbookmarks:[Bookmark];
        listofbookmarks.append(bookmark1)



        let fileManager = (NSFileManager.defaultManager())
        let directorys : [String]? = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String]

        if (directorys != nil){
            let directories:[String] = directorys!;
            let pathToFile = directories[0]; //documents directory
            let plistfile = "BookmarkArray.plist"
            let plistpath = pathToFile.stringByAppendingPathComponent(plistfile);

            if !fileManager.fileExistsAtPath(plistpath){

                println("Declaring cocoaArray")
                var cocoaArray : NSArray = listofbookmarks;
                cocoaArray.writeToFile(plistpath, atomically: true)

            }
        }

我坚持创建一个书签:var bookmark1:Bookmark=Bookmark(); “在调用中缺少参数'coder'的参数”。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

对象中唯一的初始值设定项是init (coder aDecoder: NSCoder!),因此在创建对象时,假设您正在调用该方法并且缺少NSCoder对象。

您还需要使用NSKeyedArchiver来执行序列化并将数据写入存储。 NSKeyedUnArchiver执行相反的操作。要使用它们,请遵守NSCoding协议。您还需要将您的班级标记为@objc,以便可以正确反序列化。

以下是您的代码版本,其中包含用于创建的初始化方法,但没有NSCoder,还有一些其他修复:

import Foundation

@objc(Bookmark) class Bookmark: NSObject, NSCoding { // <- added @objc and NSCoding
  var sName = ""
  var sTextPosition = ""  // beim Text scrollpos

  init(name:String, position:String) { // <- new initializer
    self.sName = name
    self.sTextPosition = position
  }

  required init (coder aDecoder: NSCoder) { // <- removed ! to adhere to NSCoding protocol
    if let name = aDecoder.decodeObjectForKey("name") as? String {
      sName = name
    }
    if let position = aDecoder.decodeObjectForKey("textposition") as? String {
      sTextPosition = position
    }
  }

  func encodeWithCoder(aCoder: NSCoder) { // <- removed ! to adhere to NSCoding protocol
    aCoder.encodeObject(sName, forKey:"name")
    aCoder.encodeObject(sTextPosition, forKey:"textposition")
  }
}

var listofbookmarks = [Bookmark]() // <- needed to create a mutable array instance
listofbookmarks.append(Bookmark(name: "myname", position: "100")) // <- new initializer

let fileManager = NSFileManager.defaultManager()

if let directories = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.AllDomainsMask, true) as? [String] {
  if !directories.isEmpty {
    let plistpath = directories[0].stringByAppendingPathComponent("BookmarkArray.plist")
    if !fileManager.fileExistsAtPath(plistpath) {
      NSKeyedArchiver.archiveRootObject(listofbookmarks, toFile: plistpath) // <- added serializer
    }
    if fileManager.fileExistsAtPath(plistpath) {
      if let decoded = NSKeyedUnarchiver.unarchiveObjectWithFile(plistpath) as? [Bookmark] {
          println(decoded[0].sName)  // => "myname"
          println(decoded[0].sTextPosition) // => "100"
      }
    }
  }
}