最新消息:20190529 VPS服务器已从腾讯云香港换为Vultr新加坡,主题仍用朋友推荐的大前端D8

[已解决]swift出错:Nil is not compatible with expected argument type NSFileManagerItemReplacementOptions

Swift crifan 1128浏览 0评论

折腾:

[已解决]swift中出错:Extra argument error in call

期间,出错:

/Users/crifan/dev/dev_root/daryun/JianDao/iOS-Client/JianDao/TCBlobDownloadManager.swift:187:145: Nil is not compatible with expected argument type ‘NSFileManagerItemReplacementOptions’

看到定义:

    /* -replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: is for developers who wish to perform a safe-save without using the full NSDocument machinery that is available in the AppKit.

    

        The `originalItemURL` is the item being replaced.

        `newItemURL` is the item which will replace the original item. This item should be placed in a temporary directory as provided by the OS, or in a uniquely named directory placed in the same directory as the original item if the temporary directory is not available.

        If `backupItemName` is provided, that name will be used to create a backup of the original item. The backup is placed in the same directory as the original item. If an error occurs during the creation of the backup item, the operation will fail. If there is already an item with the same name as the backup item, that item will be removed. The backup item will be removed in the event of success unless the `NSFileManagerItemReplacementWithoutDeletingBackupItem` option is provided in `options`.

        For `options`, pass `0` to get the default behavior, which uses only the metadata from the new item while adjusting some properties using values from the original item. Pass `NSFileManagerItemReplacementUsingNewMetadataOnly` in order to use all possible metadata from the new item.

     */

    @available(iOS 4.0, *)

    public func replaceItemAtURL(originalItemURL: NSURL, withItemAtURL newItemURL: NSURL, backupItemName: String?, options: NSFileManagerItemReplacementOptions, resultingItemURL resultingURL: AutoreleasingUnsafeMutablePointer<NSURL?>) throws

@available(iOS4.0, *)

public struct NSFileManagerItemReplacementOptions : OptionSetType {

    public init(rawValue: UInt)

   

    /* NSFileManagerItemReplacementUsingNewMetadataOnly causes -replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: to use metadata from the new item only and not to attempt to preserve metadata from the original item.

     */

    public static var UsingNewMetadataOnly: NSFileManagerItemReplacementOptions { get }

   

    /* NSFileManagerItemReplacementWithoutDeletingBackupItem causes -replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: to leave the backup item in place after a successful replacement. The default behavior is to remove the item.

     */

    public static var WithoutDeletingBackupItem: NSFileManagerItemReplacementOptions { get }

}

改为:

try NSFileManager.defaultManager().replaceItemAtURL(download.destinationURL, withItemAtURL: location, backupItemName: nil, options: NSFileManagerItemReplacementOptions.UsingNewMetadataOnly, resultingItemURL: &resultingURL)

虽然可以编译通过,但是不确定具体含义。

去改为: 0

try NSFileManager.defaultManager().replaceItemAtURL(download.destinationURL, withItemAtURL: location, backupItemName: nil, options: 0, resultingItemURL: &resultingURL)

还是不行:

/Users/crifan/dev/dev_root/daryun/JianDao/iOS-Client/JianDao/TCBlobDownloadManager.swift:187:145: Cannot convert value of type ‘Int’ to expected argument type ‘NSFileManagerItemReplacementOptions’

最终改为nil,但是错误依旧

搜:

Nil is not compatible with expected argument type ‘NSFileManagerItemReplacementOptions’

Nil is not compatible with expected argument type  NSFileManagerItemReplacementOptions

Nil is not compatible  NSFileManagerItemReplacementOptions

swift – replaceItemAtUrl working on simulator but not real iPhone – Stack Overflow

搜:

NSFileManagerItemReplacementOptions

NSFileManager Class Reference

NSFileManagerItemReplacementOptions

The constants specify the replacement behavior in replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error:.

Declaration

SWIFT

struct NSFileManagerItemReplacementOptions : OptionSetType {

    init(rawValue rawValue: UInt)

    static var UsingNewMetadataOnly: NSFileManagerItemReplacementOptions { get }

    static var WithoutDeletingBackupItem: NSFileManagerItemReplacementOptions { get }

}

OBJECTIVE-C

typedef enum : NSUInteger {

   NSFileManagerItemReplacementUsingNewMetadataOnly = 1UL << 0,

   NSFileManagerItemReplacementWithoutDeletingBackupItem = 1UL << 1

} NSFileManagerItemReplacementOptions;

Constants

NSFileManagerItemReplacementUsingNewMetadataOnly

Causes replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: to use metadata from the new item only and not to attempt to preserve metadata from the original item.

Available in iOS 4.0 and later.

NSFileManagerItemReplacementWithoutDeletingBackupItem

Causes replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: to leave the backup item in place after a successful replacement. The default behavior is to remove the item.

Available in iOS 4.0 and later.

-》看起来,NSFileManagerItemReplacementUsingNewMetadataOnly是默认的动作

-》所以还是去设置:NSFileManagerItemReplacementUsingNewMetadataOnly

-》NSFileManagerItemReplacementOptions.UsingNewMetadataOnly

最终改为如下:

    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {

        let download = self.downloads[downloadTask.taskIdentifier]!

        //var fileError: NSError?

        var resultingURL: NSURL?

//        if NSFileManager.defaultManager().replaceItemAtURL(download.destinationURL, withItemAtURL: location, backupItemName: nil, options: nil, resultingItemURL: &resultingURL, error: &fileError) {

//            download.resultingURL = resultingURL

//        } else {

//            download.error = fileError

//        }

       

        do {

            try NSFileManager.defaultManager().replaceItemAtURL(download.destinationURL, withItemAtURL: location, backupItemName: nil, options: NSFileManagerItemReplacementOptions.UsingNewMetadataOnly, resultingItemURL: &resultingURL)

           

            download.resultingURL = resultingURL

        } catch let error as NSError {

            download.error = error

        } 

    }

而通过编译。

转载请注明:在路上 » [已解决]swift出错:Nil is not compatible with expected argument type NSFileManagerItemReplacementOptions

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.112 seconds, using 18.59MB memory