書籍を購入してリストを入力したのですが、エラーが発生して動作しません。
大変申し訳ありませんが、どこが悪いのかご教授願います。
swift3
// // ViewController.swift // ICloudDemo // import UIKit class ViewController: UIViewController{ // Documentに関する情報 struct USDocInfo { static let NAME = "usdoc_test" static let EXTENSION = "us" static var LOCAL_DOCUMENTS_PATH:String? = nil static var ICLOUD_CONTAINER_PATH:String? = nil } // アプリのサンドボックスのパスを格納する変数 var localDocumentsPath: String { if let dir = USDocInfo.LOCAL_DOCUMENTS_PATH { return dir } else { let dir = NSSearchPathForDirectoriesInDomains( .documentDirectory, .userDomainMask, true)[0] + "/" USDocInfo.LOCAL_DOCUMENTS_PATH = dir return dir } } var iCloudContainerPath: String? { return USDocInfo.ICLOUD_CONTAINER_PATH } var document: USDocument! var documentURL: URL { return document.fileURL } var isFileExists = false let query = NSMetadataQuery() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. query.searchScopes = [ NSMetadataQueryUbiquitousDocumentsScope, NSMetadataQueryAccessibleUbiquitousExternalDocumentsScope] query.predicate = NSPredicate(format: "%K LIKE '*'", NSMetadataItemFSNameKey) DispatchQueue.global(qos: .default).async(execute: { if let url = FileManager.default.url(forUbiquityContainerIdentifier: nil) { print("iCloudコンテナのURL:\(url)") USDocInfo.ICLOUD_CONTAINER_PATH = url.path + "/Documents/" self.iCloudContainerDidInitialize() } }) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(ViewController.iCloudDocumentDidChange(_:)), name: NSNotification.Name.UIDocumentStateChanged, object: self.document) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDocumentStateChanged, object: self.document) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func iCloudContainerDidInitialize() { let filePath = iCloudContainerPath! + USDocInfo.NAME + "." + USDocInfo.EXTENSION let fileUrl = URL(fileURLWithPath: filePath) createDocument(fileUrl) } func createDocument(_ fileUrl: URL?) { let localFilePath = localDocumentsPath + USDocInfo.NAME + "." + USDocInfo.EXTENSION let localFileUrl = URL(fileURLWithPath : localFilePath) let doc = USDocument(fileURL : localFileUrl) doc.place = "自宅" print("自宅") doc.save(to: localFileUrl, for: .forCreating, completionHandler: { (success:Bool) in // "fatal error: unexpectedly found nil while unwrapping an Optional value" print("pass") if success { print("Documentデータを保存しました。") } else { print("Documentデータを保存できませんでした。") } }) } func iCloudDocumentDidChange(_ notification: Notification) { if self.document.documentState.contains(.inConflict){ do{ try NSFileVersion.removeOtherVersionsOfItem(at: self.document.fileURL) } catch let error { print("エラー内容:\(error)") } if let conflicts = NSFileVersion.unresolvedConflictVersionsOfItem(at: self.document.fileURL){ for fileVersion in conflicts { fileVersion.isResolved = true } } } } }
もう一つのファイルです。
swift3
// // USDocument.swift // ICloudDemo // import UIKit class USDocument: UIDocument { // Static定数の宣言 struct USFileWrapperKeys { static let IMG = "USDocuemnt.img" static let PLACE = "USDocument.place" static let DATE = "USDocument.date" static let DETAIL = "USDocument.detail" } // 記録したい内容を保持するメンバ変数を宣言します var img:UIImage? var place:String? var date:Date? var detail:String? // 上記、Documentの内容をひとまとめにパッケージするNSFileWrapperもメンバ変数として宣言します var fileWrapper:FileWrapper? // MARK: - 書き込み用のメソッド override func contents(forType typeName: String) throws -> Any { if self.fileWrapper == nil { // 空のDictonaryを渡している self.fileWrapper = FileWrapper(directoryWithFileWrappers:[:]) } if let place = self.place , let data = place.data(using: String.Encoding.utf8) { let fw = FileWrapper(regularFileWithContents:data) fw.preferredFilename = USFileWrapperKeys.PLACE self.fileWrapper?.addFileWrapper(fw) } if let img = self.img , let data = UIImageJPEGRepresentation(img, 1.0) { let fw = FileWrapper(regularFileWithContents:data) fw.preferredFilename = USFileWrapperKeys.IMG self.fileWrapper?.addFileWrapper(fw) } if let date = self.date { let data = NSKeyedArchiver.archivedData(withRootObject: date) let fw = FileWrapper(regularFileWithContents:data) fw.preferredFilename = USFileWrapperKeys.DATE self.fileWrapper?.addFileWrapper(fw) } if let detail = self.detail , let data = detail.data(using: String.Encoding.utf8) { let fw = FileWrapper(regularFileWithContents:data) fw.preferredFilename = USFileWrapperKeys.DETAIL self.fileWrapper?.addFileWrapper(fw) } return self.fileWrapper as Any } }
まだ回答がついていません
会員登録して回答してみよう