无法转换'inout NSNumber类型的值?'到预期的参数类型'AutoreleasingUnsafeMutablePointer '错误

问题描述 投票:-1回答:1

我使用此脚本来检查从iCloud下载的文件是否可用。但不幸的是,我在某些代码行中遇到了错误Cannot convert value of type 'inout NSNumber?' to expected argument type 'AutoreleasingUnsafeMutablePointer<AnyObject?>'。请帮助我解决此问题,因为这是我第一次创建代码来检查下载的文件是否在icloud中可用。

请参考下面的图片作为错误示例,以下代码也可供您参考。希望你能帮助我。谢谢。

Sample screenshot of error

 //-------------------------------------------------------------------
// ダウンロードできるか判定 Judgment or can be downloaded
//-------------------------------------------------------------------

func downloadFileIfNotAvailable(_ file: URL?) -> Bool {
    var isIniCloud: NSNumber? = nil
    do {
        try (file as NSURL?)?.getResourceValue(&isIniCloud, forKey: .isUbiquitousItemKey)

        if try (file as NSURL?)?.getResourceValue(&isIniCloud, forKey: .isUbiquitousItemKey) != nil {
            if isIniCloud?.boolValue ?? false {
                var isDownloaded: NSNumber? = nil
                if try (file as NSURL?)?.getResourceValue(&isDownloaded, forKey: .ubiquitousItemIsDownloadedKey) != nil {
                    if isDownloaded?.boolValue ?? false {
                        return true
                    }
                    performSelector(inBackground: #selector(startDownLoad(_:)), with: file)
                    return false
                }
            }
        }
    } catch {
    }
    return true
}
ios swift icloud nsurl
1个回答
0
投票

看来您复制并粘贴了一些非常老的代码。此外,这是Swift,而不是Objective-C。不要使用NSURL或getResourceValue。您的代码应看起来像这样:

    if let rv = try file?.resourceValues(forKeys: [.isUbiquitousItemKey]) {
        if let isInCloud = rv.isUbiquitousItem {
            // and so on
        }
    }

依此类推;相同的模式应用于其他键。注意,也没有.ubiquitousItemIsDownloadKey。您可以压缩这样的内容:

    if let rv = try file?.resourceValues(
        forKeys: [.isUbiquitousItemKey, .ubiquitousItemDownloadingStatusKey]) {
            if let isInCloud = rv.isUbiquitousItem {
                if let status = rv.ubiquitousItemDownloadingStatus {
                    if status == .downloaded {

                    }
                }
            }
    }
© www.soinside.com 2019 - 2024. All rights reserved.