iOS Swift/error 해결 방법
Initializer for conditional binding must have Optional type, not 'String'
호두빵
2022. 12. 22. 09:48
상단의 코드에서 if let docuPath = directory.path로 잡아주게 되면 제목과 같이 조건부 바인딩의 초기자(initializer)는 옵셔널형이어야하며, String일수 없다는 에러가 뜹니다. 있을지 없을지도 모르는데 어떻게 저렇게 쓰니? 하는 것이죠...
let docuPath = directory.path
if !docupath.isEmpty {
let fileNames = try FileManager.default.contentsOfDirectory(atPath: docuPath)
for fileName in fileNames {
if fileName == named {
let filePathName = "\(docuPath)/ \(fileName)"
try FileManager.default.removeItem(atPath: filePathName)
onSuccess(true)
return
}
}
}
이런 경우에는 아래와 같이 우회해서 코드를 적어주면 에러도 사라지고 빌드가 됩니다.