NSFileCoordinator coordinateWritingItemAtURL无法调用' coordinateWritingItemAtURL'

时间:2015-09-27 19:26:01

标签: ios xcode swift

我正在尝试在NSFileCoordinator中撰写内容,但它会引发错误。 我尝试了几种方法,但没有一种方法有效。

我的代码:

private func saveValues(note :String){
    fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil, byAccessor: { (newUrl: NSURL!) in
        self.notes.addObject(note)
        let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
        let success = saveData.writeToURL(newURL, atomically: true)
    })

    fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in
        self.notes.addObject(note)
        let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
        let success = saveData.writeToURL(newURL, atomically: true)
    }
    print("===##==> ",note)
}

以上两个代码都失败了。 enter image description here

整个代码看起来 -

import UIKit

class UpdateViewController: UIViewController, NSFilePresenter {
    @IBOutlet weak var lblView: UILabel!
    @IBOutlet weak var tfInputField: UITextField!

    var fileCoordinator = NSFileCoordinator()
    var notes :NSMutableArray = NSMutableArray()
    var presentedItemURL: NSURL? {
        let groupURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.navigateproje")
        let fileURL = groupURL?.URLByAppendingPathComponent("notes.bin")
        return fileURL!
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func btnPressed(sender: UIButton) {    
        print("print ",tfInputField.text)
        lblView.text = tfInputField.text
        saveValues(tfInputField.text!)
    }

    private func saveValues(note :String){    
        fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil, byAccessor: { (newUrl: NSURL!) in
            self.notes.addObject(note)
            let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
            let success = saveData.writeToURL(newURL, atomically: true)
        })
        fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: nil, error: nil) { ( newURL :NSURL!) -> Void in  
            self.notes.addObject(note)
            let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
            let success = saveData.writeToURL(newURL, atomically: true)
        }
        print("===##==> ",note)
    }

    var presentedItemOperationQueue: NSOperationQueue {    
        return NSOperationQueue.mainQueue()
    }
}

2 个答案:

答案 0 :(得分:2)

我解决了以下问题 -

https://forums.developer.apple.com/thread/9301

  fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: NSFileCoordinatorWritingOptions.ForMoving, error: &writingError, byAccessor: { (coordinator) in

            self.notes.addObject(note)
            let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
            let success = saveData.writeToURL(coordinator, atomically: true)
        })

实际的麻烦是选项类型是nil,必须是NSFileCoordinatorWritingOptions.ForMerging类型,

所以即使是以下作品 -

    fileCoordinator.coordinateWritingItemAtURL(presentedItemURL!, options: NSFileCoordinatorWritingOptions.ForMerging, error: nil) { ( newURL :NSURL!) -> Void in

        self.notes.addObject(note)

        let saveData = NSKeyedArchiver.archivedDataWithRootObject(self.notes)
        let success = saveData.writeToURL(newURL, atomically: true)
    }

答案 1 :(得分:0)

byAccessor闭包中的返回类型被声明为非可选

func coordinateReadingItemAtURL(_ url: NSURL,
                    options options: NSFileCoordinatorReadingOptions,
                      error outError: NSErrorPointer,
                 byAccessor reader: (NSURL) -> Void)