The IOS Compute folder contains the size of all files
File Size:
Calculate Output Size:
Method One:
classfunc fileSize (path:nsstring!) -Float64{
let filemanager =nsfilemanager()
var size:Float64 =0.0
do {
let arr = try FileManager. contentsofdirectoryatpath (path as string )
if arr. Count == 0 {return size} // when a folder has no files
for index in0... arr. Count - 1 {
let fullPath = path. stringbyappendingpathcomponent (Arr[index])
var isdir: objcbool = false //isdir
if FileManager. fileexistsatpath (FullPath, isdirectory: &isdir) &&!isd ir { Span style= "COLOR: #1d9421" >// No folder calculated size
let fileattributedic =try! filemanager. Attributesofitematpath (fulllpath)
size + = (fileattributedic[nsfilesize]?. Doublevalue)!
}else {
size + = Filemanagertool . fileSize (FullPath) // calculate its size recursively
}
}
return size
}catch {
return size
}
}
Method Two: classfuncfileSize2 (path:NSString!) -Float64 {
let filemanager =nsfilemanager()
var size:Float64 =0.0
let arr = filemanager. subpathsatpath As string )
if arr!. Count == 0 {return size} // when a folder has no files
for index in0... arr!. Count - 1 {
let fullPath = path. stringbyappendingpathcomponent (arr![ Index])
var Isdir: objcbool = false //isdir
if filemanager. Fileexistsatpath (FullPath, isdirectory: &isdir) &&!isdir {// not folder calculated size
let fileattributedic =try! filemanager. Attributesofitematpath (Fullpath)
size + = (fileattributedic[nsfilesize]?. Doublevalue)!
}
}
return size
}
Two ways to find a path:Contentsofdirectoryatpath: To get the folder and file path under the current folder in a non-recursive way Subpathsatpath: Recursively get all folders and file paths under the current folder
The IOS Compute folder contains the size of all files