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)

  

posted @ 2017-08-04 09:15  昨天的李小白  阅读(728)  评论(0编辑  收藏  举报