SWIFT3 – Set URL Resource Values ​​in SWIFT 3

I am trying to convert Apple’s “ShapeEdit” example to Swift 3, and I cannot understand the change to the setResourceValue of the URL.

Apple(Swift 2) The example contains the following code:

// 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 o perations: \(error)")
}
}

setResourceValue(value:forKey:) seems to have been replaced by setResourceValues(), but I cannot set it. So far I What you have is:

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.open DocumentAtURL(writeIntent.URL)
}
} catch {
fatalError("Unexpected error during trivial file operations: \(error)")
}
}

I can't find any documentation other than Xcode "jump to definition"

Sets the resource value identified by a given resource key.

This method writes the new resource values ​​out to the backing store. Attempts to set a read-only resource property or to set a resource property not supported by the resource are ignored and are not considered errors. This method is currently applicable only to URLs for file system resources.

URLResourceValues keeps track of which of its properties have been set. Those values ​​are the ones used by this function to
determine which properties to write.

public mutating func setResourceValues(_ values: URLResourceValues) throws

Is anyone concerned about setResourceValue(s) Any understanding of the changes?

The current declaration of NSFileAccessIntent.URL is

public var url: URL {get }

This is a read-only property.

Since Swift 3 URL is a structure, you cannot Change the value to call the mutating method. To modify the URL, first assign it to var.

var url = intent.URL
url.setResourceValues(...)< /pre>

Then create a new intent from the modified URL.

I tried to convert Apple’s "ShapeEdit" example to Swift 3, which I can’t understand Changes to the setResourceValue of the URL.

The Apple(Swift 2) example contains the following code:

// 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: wr iteIntent.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: ) Seems to have been replaced by setResourceValues(), but I cannot set it. What I have so far is:

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)")
}
}

Except for Xcode "jump to definition", I can't find any documentation

Sets the resource value identified by a given resource key.

This method writes the new resource values ​​out to the backing store. Attempts to set a read-only resource property or to set a resource property not supported by the resource are ignored and are not considered errors. This method is currently applicable only to URLs for file system resources.

URLResourceValues keeps track of which of its properties have been set. Those values ​​are the ones used by this function to
determine which properties to write.

public mutating func setResourceValues(_ values: URLResourceValues) throws< /p>

Does anyone have any knowledge about the change of setResourceValue(s)?

The current declaration of NSFileAccessIntent.URL is

public var url: URL {get} 

This is a read-only property.

Since the Swift 3 URL is a structure, you cannot call the mutating method on the immutable value returned from the getter. To modify the URL, first add It is assigned to var.

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

and then from the modified URL Create a new intent in .

Leave a Comment

Your email address will not be published.