文件操作
import Foundation
let manger = FileManager.default
let url = URL(string: "/path/to/your/path")
let enumerator = manger.enumerator(at: url!, includingPropertiesForKeys: nil, options: .skipsHiddenFiles, errorHandler: nil)
while let element = enumerator?.nextObject() as? URL {
print(element)
}
import Foundation
let manger = FileManager.default
let url = URL(string: "/path/to/your/path")
// include sub folders
let enumerator = manger.enumerator(at: url!, includingPropertiesForKeys: nil, options: .skipsHiddenFiles, errorHandler: nil)
// order by name
var enumeratorArray: [URL] = []
while let element = enumerator?.nextObject() as? URL { enumeratorArray.append(element) }
let enumeratorSorted = enumeratorArray.sorted(by: {(s1, s2) in
return s1.path < s2.path
})
for item in enumeratorSorted {
print(item)
}
// exclude sub folders
let items: [URL] = try! manger.contentsOfDirectory(at: url!, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
// order by name
let itemsSorted = items.sorted(by: {(s1, s2) in
return s1.path < s2.path
})
for item in itemsSorted {
print(item)
}
import Foundation
let manager = FileManager.default
let data = manager.contents(atPath: "/path/to/your/file")
let string = String(data: data!, encoding: String.Encoding.utf8)
print(string)