首页 > 解决方案 > 无法转换“inout NSNumber”类型的值?到预期的参数类型'AutoreleasingUnsafeMutablePointer' 错误

问题描述

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

请参考下图作为错误示例,下面还有代码供您参考。希望你能帮助我。谢谢你。

错误截图示例

 //-------------------------------------------------------------------
// ダウンロードできるか判定 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
}

标签: iosswifticloudnsurl

解决方案


看起来您复制并粘贴了一些非常旧的代码。此外,这是 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 {

                    }
                }
            }
    }

推荐阅读