var image = UIImage()
if image != nil {
print("this image is not nil")
}
Comparing non-optional value of type 'UIImage' to 'nil' always returns false
image를 옵셔널로 정의하지 않은 상태에서 nil인지 아닌지를 물어보는 것은 의미가 없습니다. 해당 이미지를 nil인지 아닌지를 확인하기 위해서는 선언할 때에 아래와 같이 옵셔널로 남겨둔 다음 처리가 가능합니다.
var image: UIImage? = UIImage()
if image != nil {
print("this image is not nil")
}
만약 불가피한 경우라면 아래와 같이 우회하는 방법을 써서 확인이 가능합니다.
var image = UIImage()
if image.size.width == 0 {
print("this image is unavailable")
} else {
print("this image is available")
}
'iOS Swift > Today I Learned' 카테고리의 다른 글
tableView의 header, footer view 색 변경하기 (0) | 2023.01.05 |
---|---|
filter 및 tableView section 활용하기 (0) | 2022.12.30 |
CIImage, CGImage, UIImage 변환 및 개념 정리 (0) | 2022.12.28 |
DateFormatter 활용해보기 (0) | 2022.12.27 |
이미지를 circle로 만들어주는 clipToBounds (0) | 2022.12.22 |