swift 获取文件大小

//获取文件大小
    func getSize(url: URL)->UInt64
    {
        var fileSize : UInt64 = 0
         
        do {
            let attr = try FileManager.default.attributesOfItem(atPath: url.path)
            fileSize = attr[FileAttributeKey.size] as! UInt64
             
            let dict = attr as NSDictionary
            fileSize = dict.fileSize()
        } catch {
            print("Error: \(error)")
        }
         
        return fileSize
    }

获取之前最好先判断下文件是否存在

FileManager.default.fileExists(atPath: path)

你可能感兴趣的:(swift 获取文件大小)