美文网首页
swift 获取文件大小

swift 获取文件大小

作者: 昨天的李小白 | 来源:发表于2017-08-04 09:18 被阅读0次
//获取文件大小
    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 获取文件大小

      本文链接:https://www.haomeiwen.com/subject/acfvlxtx.html