swift 获取文件大小

2017-08-04  本文已影响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)
上一篇 下一篇

猜你喜欢

热点阅读