How to get the file size given a path?

后端 未结 10 1199
自闭症患者
自闭症患者 2020-11-30 20:22

I have a path to file contained in an NSString. Is there a method to get its file size?

10条回答
  •  被撕碎了的回忆
    2020-11-30 20:56

    In Swift 3.x and above you can use:

    do {
        //return [FileAttributeKey : Any]
        let attr = try FileManager.default.attributesOfItem(atPath: filePath)
        fileSize = attr[FileAttributeKey.size] as! UInt64
    
        //or you can convert to NSDictionary, then get file size old way as well.
        let attrDict: NSDictionary = try FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary
        fileSize = dict.fileSize()
    } catch {
        print("Error: \(error)")
    }
    

提交回复
热议问题