質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

Q&A

1回答

1940閲覧

swift3 "fatal error: unexpectedly found nil while unwrapping an Optional value"が出る

harima

総合スコア17

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

iPhone

iPhoneとは、アップル社が開発・販売しているスマートフォンです。 同社のデジタルオーディオプレーヤーiPodの機能、電話機能、インターネットやメールなどのWeb通信機能の3つをドッキングした機器です。

0グッド

0クリップ

投稿2017/05/03 04:45

編集2017/05/03 04:48

雑誌を見てiCloud Documentの実験をしているのですが、実行エラーが発生して動作確認ができません。doc.save(to...の122行目です。

// // ViewController.swift // ICloudDemo // // Created by Comeluck on 2017/04/26. // Copyright © 2017年 Comeluck. All rights reserved. // 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() // MARK: - Viewのライフサイクル 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. } // 非同期でのコンテナURL取得の結果として呼ばれます func iCloudContainerDidInitialize() { let filePath = iCloudContainerPath! + USDocInfo.NAME + "." + USDocInfo.EXTENSION let fileUrl = URL(fileURLWithPath: filePath) // ファイルの存在チェック isFileExists = FileManager.default.fileExists(atPath: filePath) if isFileExists { print("iCloudコンテナにDocumentデータがあります: \(filePath)") document = USDocument(fileURL:fileUrl) document.open(completionHandler: { (success:Bool) -> Void in if success { print("Documentを開きました。place: \(String(describing: self.document.place))") } else { print("Documentを開けませんでした。") } }) } else { print("iCloudコンテナにDocumentデータはありません。") // Documentを新規作成し、iCloudコンテナに保存 createDocument(fileUrl) // dispatch_async(dispatch_get_main_queue(), { // NSNotificationCenter.defaultCenter().addObserver(self, selector: "metadataQueryDidChange:", name: NSMetadataQueryDidFinishGatheringNotification, object: nil) // self.query.startQuery() // }) } } // func createDocument(_ fileUrl: URL?) { 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データを保存しました。") let error:NSError? = nil DispatchQueue.global(qos: .default).async { do{ try FileManager.default.setUbiquitous(true, itemAt: localFileUrl, destinationURL: fileUrl!) } catch let error { print("エラー内容:\(error)") } if success { print("iCloudコンテナへの移動に成功しました") } else { if let err = error { // print("iCloudコンテナへの移動に失敗しました: \(err.description)") print("iCloudコンテナへの移動に失敗しました: \(err.localizedDescription)") } } } } else { print("Documentデータを保存できませんでした。") } }) } // MARK: - Notification func metadataQueryDidChange(_ notification: Notification) { print("metadataQueryDidChange") query.stop() NotificationCenter.default.removeObserver(self, name: NSNotification.Name.NSMetadataQueryDidFinishGathering, object: nil) let metadataItems = query.results as! [NSMetadataItem] let urls = metadataItems.map({ (item: NSMetadataItem) -> URL in let url = item.value(forAttribute: NSMetadataItemURLKey) as! URL print("iCloud Storage: \(url)") return url }).filter({ (url: URL) -> Bool in if let fileName = url.path.components(separatedBy: "/").last { return fileName == USDocInfo.NAME + "." + USDocInfo.EXTENSION } else { return false } }) if 0 < urls.count { self.document = USDocument(fileURL: urls.first!) self.document.open(completionHandler: { (success:Bool) in if success { print("Documentを開きました。place: \(String(describing: self.document.place))") } else { print("Documentを開けませんでした。") } }) } else { print("該当ファイルがありません") } } func metadataQueryDidUpdate(_ notification: Notification) { print("metadataQueryDidUpdate") query.disableUpdates() var insertedURLs = [URL]() var removedURLs = [URL]() var updatedURLs = [URL]() let metadataItemToURLTransform: (NSMetadataItem) -> URL = { metadataItem in return metadataItem.value(forAttribute: NSMetadataItemURLKey) as! URL } let insertedMetadataItems = notification.userInfo?[NSMetadataQueryUpdateAddedItemsKey] as! [NSMetadataItem]? if let insertedMetadataItems = insertedMetadataItems { insertedURLs += insertedMetadataItems.map(metadataItemToURLTransform) } let removedMetadataItems = notification.userInfo?[NSMetadataQueryUpdateRemovedItemsKey] as! [NSMetadataItem]? if let removedMetadataItems = removedMetadataItems { removedURLs += removedMetadataItems.map(metadataItemToURLTransform) } let updatedMetadataItems = notification.userInfo?[NSMetadataQueryUpdateChangedItemsKey] as! [NSMetadataItem]? if let updatedMetadataItems = updatedMetadataItems { let completelyDownloadedUpdatedMetadataItems = updatedMetadataItems.filter { updatedMetadataItem in let downloadStatus = updatedMetadataItem.value(forAttribute: NSMetadataUbiquitousItemDownloadingStatusKey) as! String return downloadStatus == NSMetadataUbiquitousItemDownloadingStatusCurrent } updatedURLs += completelyDownloadedUpdatedMetadataItems.map(metadataItemToURLTransform) } query.enableUpdates() } 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 } } } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

acevif

2017/05/04 01:44

質問サイトに出すソースとしては長すぎ、また、インデントなどが汚いように思います。同じ問題が発生する、最小限のソースコードになるまで削って、また不要なコメントなども削除し、きれいに整形してください。
harima

2017/05/04 02:24

分かりました。申し訳ありませんでした。
acevif

2017/05/04 02:30 編集

いえいえ。問題点が絞り込まれた良い質問ができるようになるには、相当な訓練がいるものです。そもそも問題点を絞り込めるようになると、問題を自力で解決できてしまうことが多いですし。指摘されながら徐々に学んでいけば良いことで、そのための質問サイトです。あやまることではありませんよ。まずはソースのコピーを作り、問題に無関係なところを排除していってください。
harima

2017/05/04 04:27

ありがとうございます。出直してきます。
guest

回答1

0

doc または localFileUrl がnilなのではありませんか?
saveを実行する直前でデバッガーでとめ、チェックしてください。

投稿2017/05/04 01:44

acevif

総合スコア59

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問