我正在尝试用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()
}
}发布于 2015-09-28 03:39:28
我解决了这个问题-
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)
})实际的问题是option类型是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)
}发布于 2015-09-28 03:36:39
byAccessor闭包中的返回类型被声明为非可选
func coordinateReadingItemAtURL(_ url: NSURL,
options options: NSFileCoordinatorReadingOptions,
error outError: NSErrorPointer,
byAccessor reader: (NSURL) -> Void)https://stackoverflow.com/questions/32811944
复制相似问题