我正在尝试在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)
}
整个代码看起来 -
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()
}
}
答案 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)