首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSFileCoordinator coordinateWritingItemAtURL无法调用“coordinateWritingItemAtURL”

NSFileCoordinator coordinateWritingItemAtURL无法调用“coordinateWritingItemAtURL”
EN

Stack Overflow用户
提问于 2015-09-28 03:26:01
回答 2查看 1.4K关注 0票数 2

我正在尝试用NSFileCoordinator编写内容,但它抛出了一个错误。我尝试了几种方法,但都不管用。

我的代码:

代码语言:javascript
复制
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)
}

以上两个代码都失败了。

整个代码看起来-

代码语言:javascript
复制
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()
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-09-28 03:39:28

我解决了这个问题-

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

代码语言:javascript
复制
  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类型,

所以即使是跟随作品-

代码语言:javascript
复制
    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)
    }
票数 3
EN

Stack Overflow用户

发布于 2015-09-28 03:36:39

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

代码语言:javascript
复制
func coordinateReadingItemAtURL(_ url: NSURL,
                    options options: NSFileCoordinatorReadingOptions,
                      error outError: NSErrorPointer,
                 byAccessor reader: (NSURL) -> Void)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32811944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档