반응형
NSFileManager fileExistsAtPath : isDirectory 및 swift
fileExistsAtPath:isDirectory:
Swift로 함수를 사용하는 방법을 이해하려고하는데 완전히 길을 잃었습니다.
이것은 내 코드 예제입니다.
var b:CMutablePointer<ObjCBool>?
if (fileManager.fileExistsAtPath(fullPath, isDirectory:b! )){
// how can I use the "b" variable?!
fileManager.createDirectoryAtURL(dirURL, withIntermediateDirectories: false, attributes: nil, error: nil)
}
b
MutablePointer 의 값에 어떻게 액세스 할 수 있는지 이해할 수 없습니다 . YES
또는로 설정되어 있는지 알고 싶으면 어떻게 NO
합니까?
두 번째 매개 변수 의 유형 은 변수 UnsafeMutablePointer<ObjCBool>
의 주소 를 전달해야 함을 의미 ObjCBool
합니다. 예:
var isDir : ObjCBool = false
if fileManager.fileExistsAtPath(fullPath, isDirectory:&isDir) {
if isDir {
// file exists and is a directory
} else {
// file exists and is not a directory
}
} else {
// file does not exist
}
Swift 3 및 Swift 4 업데이트 :
let fileManager = FileManager.default
var isDir : ObjCBool = false
if fileManager.fileExists(atPath: fullPath, isDirectory:&isDir) {
if isDir.boolValue {
// file exists and is a directory
} else {
// file exists and is not a directory
}
} else {
// file does not exist
}
사용하기 쉽도록 다른 답변을 개선하려고했습니다.
extension URL {
enum Filestatus {
case isFile
case isDir
case isNot
}
var filestatus: Filestatus {
get {
let filestatus: Filestatus
var isDir: ObjCBool = false
if FileManager.default.fileExists(atPath: self.path, isDirectory: &isDir) {
if isDir.boolValue {
// file exists and is a directory
filestatus = .isDir
}
else {
// file exists and is not a directory
filestatus = .isFile
}
}
else {
// file does not exist
filestatus = .isNot
}
return filestatus
}
}
}
참고 URL : https://stackoverflow.com/questions/24696044/nsfilemanager-fileexistsatpathisdirectory-and-swift
반응형
'Development Tip' 카테고리의 다른 글
인라인 블록으로 새 줄을 끊으셨습니까? (0) | 2020.11.19 |
---|---|
Swift Playground는 UIKit을 지원합니까? (0) | 2020.11.19 |
jQuery UI 대화 상자 OnBeforeUnload (0) | 2020.11.19 |
adb에 대한 연결이 끊어져 심각한 오류가 발생했습니다. (0) | 2020.11.19 |
jquery로 div 하단 위치 찾기 (0) | 2020.11.19 |