2016-08-17 11 views
3

Appleの"ShapeEdit"の例をSwift 3に変換しようとしていますが、URLsetResourceValueへの変更を自分の頭で行うことはできません。Swift 3のURLリソース値を設定しています

アップル(スウィフト2)の例では、このコードを持っています

// Coordinate reading on the source path and writing on the destination path to copy. 
let readIntent = NSFileAccessIntent.readingIntentWithURL(templateURL, options: []) 
let writeIntent = NSFileAccessIntent.writingIntentWithURL(target, options: .ForReplacing) 

NSFileCoordinator().coordinateAccessWithIntents([readIntent, writeIntent], queue: self.coordinationQueue) { error in 
    if error != nil { return } 
    do { 
     try fileManager.copyItemAtURL(readIntent.URL, toURL: writeIntent.URL)  
     try writeIntent.URL.setResourceValue(true, forKey: NSURLHasHiddenExtensionKey)  
     NSOperationQueue.mainQueue().addOperationWithBlock { 
      self.openDocumentAtURL(writeIntent.URL) 
     } 
    } catch { 
     fatalError("Unexpected error during trivial file operations: \(error)") 
    } 
} 

setResourceValue(value: forKey:)setResourceValues()によって置き換えられているようだが、私はそれを設定することはできません。私はこれまで持っていることはこれです:

let readIntent = NSFileAccessIntent.readingIntent(with: templateURL, options: []) 
let writeIntent = NSFileAccessIntent.writingIntent(with: target, options: .forReplacing) 

NSFileCoordinator().coordinate(with: [readIntent, writeIntent], queue: self.coordinationQueue) { error in 
    if error != nil { return }     
    do { 
     try fileManager.copyItem(at: readIntent.url, to: writeIntent.url) 
     var resourceValues: URLResourceValues = URLResourceValues.init() 
     resourceValues.hasHiddenExtension = true 
     // *** Error on next line *** 
     try writeIntent.url.setResourceValues(resourceValues) 
     // Cannot use mutating member on immutable value: 'url' is a get-only property 

     OperationQueue.main.addOperation { 
      self.openDocumentAtURL(writeIntent.URL) 
     } 
    } catch { 
     fatalError("Unexpected error during trivial file operations: \(error)") 
    }  
} 

私は与えられたリソースキーによって識別されたリソースの値を設定します

を言うのXcode「の定義にジャンプする」、以外のドキュメントを見つけることができません。

このメソッドは、新しいリソース値をバッキングストアに書き込みます。読み取り専用のリソースプロパティを設定しようとしたり、リソースでサポートされていないリソースプロパティを設定しようとする試みは無視され、エラーとはみなされません。このメソッドは現在、ファイルシステムリソースのURLにのみ適用されます。

URLResourceValuesは、どのプロパティが設定されているかを追跡します。これらの値は、この関数がどのプロパティを書き込むかを決定するために使用される値です( )。

public mutating func setResourceValues(_ values: URLResourceValues) throws

誰もがsetResourceValue(s)への変更に任意の洞察力を持っていますか?

答えて

6

NSFileAccessIntent.URLの現在の宣言は、それは読み取り専用のプロパティです

public var url: URL { get } 

です。

Swift 3 URLstructであるため、ゲッターから返された不変の値に対して突然変異のメソッドを呼び出すことはできません。 URLを変更するには、最初にvarに割り当てます。

var url = intent.URL 
url.setResourceValues(...) 

次に、変更されたURLから新しいインテントを作成します。

+0

新しいものを学ぶときには、古いものにはなく、エラーがあると想定します。あなたはほぼ正しいですが、 'intent.url'は読み取り専用なので、3行目は機能しません。しかし、このアイデアは、変更されたURLでインテントを正しく初期化することができます。 - intent = NSFileAccessIntent.writingIntent(:url、options:.forReplacing) 'あなたは答えを更新したいのですか?そうでしょうか? – Grimxn

+0

代わりにあなた自身の答えを置き、私はこの1つを削除します:) – Sulthan

+0

いいえ、あなたはそれを解決しました! – Grimxn

4

受け入れ答えは便利ですが、コードを作業がさらに良いです

do { 
    var resourceValues = URLResourceValues() 
    resourceValues.isExcludedFromBackup = true 
    try fileUrl.setResourceValues(resourceValues) 
} catch _{ 
} 

@Sulthanが述べたように、fileURLは変更可能でなければなりません。

関連する問題