当前位置 : 主页 > 手机开发 > 其它 >

swift3 – 为什么这个简单的iCloud存储应用程序失败了?

来源:互联网 收集:自由互联 发布时间:2021-06-11
这是我第一次制作iCloud应用程序,我正在关注 this tutorial(以及关于如何设置iCloud应用程序的 this supplemental tutorial).它所做的只是接受文本(通过“文本视图”对象),并将该文本(通过UIButton
这是我第一次制作iCloud应用程序,我正在关注 this tutorial(以及关于如何设置iCloud应用程序的 this supplemental tutorial).它所做的只是接受文本(通过“文本视图”对象),并将该文本(通过UIButton)保存到存储在iCloud中的文档.我已经到了教程的最后,当我测试它时,它崩溃了.好吧,我不应该说“崩溃”,因为当我测试其他应用程序时,Xcode结束了测试,这不是这里的情况.应用程序继续运行,但我无法执行任何操作(我无法编写任何文本或使用“保存”按钮保存).

这是ViewController.swift的代码

import UIKit

class ViewController: UIViewController
{
    @IBOutlet weak var textView: UITextView!

    var document: MyDocument?
    var documentURL: URL?
    var ubiquityURL: URL?
    var metaDataQuery: NSMetadataQuery?



    func metadataQueryDidFinishGathering(notification: NSNotification) -> Void
    {
        let query: NSMetadataQuery = notification.object as! NSMetadataQuery

        query.disableUpdates()

        NotificationCenter.default.removeObserver(self,
                                                  name: NSNotification.Name.NSMetadataQueryDidFinishGathering,
                                                  object: query)

        query.stop()

        let resultURL = query.value(ofAttribute: NSMetadataItemURLKey,
                                    forResultAt: 0) as! URL

        if query.resultCount == 1 {
            let resultURL = query.value(ofAttribute: NSMetadataItemURLKey,
                                        forResultAt: 0) as! URL

            document = MyDocument(fileURL: resultURL as URL)

            document?.open(completionHandler: {(success: Bool) -> Void in
                if success {
                    print("iCloud file open OK")
                    self.textView.text = self.document?.userText
                    self.ubiquityURL = resultURL as URL
                } else {
                    print("iCloud file open failed")
                }
            })
        } else {
            document = MyDocument(fileURL: ubiquityURL!)

            document?.save(to: ubiquityURL!,
                           for: .forCreating,
                           completionHandler: {(success: Bool) -> Void in
                            if success {
                                print("iCloud create OK")
                            } else {
                                print("iCloud create failed")
                            }
            })
        }
    }



    override func viewDidLoad()
    {
        super.viewDidLoad()

        let filemgr = FileManager.default

        ubiquityURL = filemgr.url(forUbiquityContainerIdentifier: nil)

        guard ubiquityURL != nil else {
            print("Unable to access iCloud Account")
            print("Open the Settings app and enter your Apple ID into iCloud settings")
            return
        }

        ubiquityURL = ubiquityURL?.appendingPathComponent(
            "Documents/savefile.txt")

        metaDataQuery = NSMetadataQuery()

        metaDataQuery?.predicate =
            NSPredicate(format: "%K like 'savefile.txt'",
                        NSMetadataItemFSNameKey)
        metaDataQuery?.searchScopes =
            [NSMetadataQueryUbiquitousDocumentsScope]

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(
                                                ViewController.metadataQueryDidFinishGathering),
                                               name: NSNotification.Name.NSMetadataQueryDidFinishGathering,
                                               object: metaDataQuery!)

        metaDataQuery!.start()
    }

    @IBAction func saveDocument(_ sender: AnyObject)
    {

        document!.userText = textView.text

        document?.save(to: ubiquityURL!,
                       for: .forOverwriting,
                       completionHandler: {(success: Bool) -> Void in
                        if success {
                            print("Save overwrite OK")
                        } else {
                            print("Save overwrite failed")
                        }
        })
    }

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


}

这是应用程序“崩溃”时的输出:

2017-03-15 15:12:06.531223 Gaethr2[2708:734758] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMetadataQuery valueOfAttribute:forResultAtIndex:]: index (0) out of bounds (0)'
*** First throw call stack:
(0x1876951b8 0x1860cc55c 0x187695100 0x188125460 0x100102ffc 0x10010457c 0x18762eb10 0x18762e214 0x18762df90 0x18769db8c 0x18756fe64 0x1880a4e0c 0x188123268 0x18762eb10 0x18762e214 0x18762df90 0x18769db8c 0x18756fe64 0x1880a4e0c 0x19940d3e4 0x19940f29c 0x187642a44 0x187642240 0x187640094 0x18756e2b8 0x189022198 0x18d5b57fc 0x18d5b0534 0x100107720 0x1865515b8)
libc++abi.dylib: terminating with uncaught exception of type NSException

这里有一些潜在问题的截图:

enter image description here

Xcode希望我摆脱“让resultURL”这样做(如下所示),希望能解决问题,但事实并非如此.

enter image description here

我真的不在这里,所以我只是按照教程.我真的很感激一些帮助.如果我需要添加更多信息,请与我们联系.我不知道问题是什么,所以我可以省略一些重要的信息.

删除此行:

let resultURL = query.value(ofAttribute: NSMetadataItemURLKey,
                                forResultAt: 0) as! URL
网友评论