简体   繁体   English

为什么值突然变成零?

[英]Why does a value suddenly become nil?

I'm working on a project where I want to get an image for a particular identifier. 我正在一个项目中,我要获取特定标识符的图像。 I have the following function: 我有以下功能:

func imageForIdentifier(identifier: String?) -> UIImage? {
        if identifier == nil || identifier! == "" {
            return nil
        }
        let directory = directoryForID(identifier!)
        if let img = imgInCache.objectForKey(identifier!) as? UIImage {
            return img
        }
        if let img = NSData(contentsOfFile: directory) {
            return UIImage(data: img)
        }
        return nil
    }

...and the directoryForID function... ...以及directoryForID函数...

func directoryForID(identifier: String) -> String {
        let directoryPath: NSURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentationDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first!
        return (directoryPath.URLByAppendingPathComponent(identifier).path!)
    }

If I pass an image identifier it should create a directory and in the last if-Statement it should return a UIImage of the data from this directory. 如果我传递图像标识符,它应该创建一个目录,并在最后的if语句中,它应该从该目录返回数据的UIImage However it skips all of the if-Statements and returns nil. 但是,它将跳过所有的if-Statements并返回nil。 How is this possible even though I pass a correct identifier? 即使我传递了正确的标识符,这怎么可能?

Thank you in advance. 先感谢您。

The reason is how you are testing the nil-ability or emptiness of the string object identifier : 原因是您如何测试字符串对象标识符的 性:

identifier.length can be helpful to validate the content of the identifier... identifier.length可能有助于验证标识符的内容...

Edited answer. 编辑答案。

func imageForIdentifier(identifier: String?) -> UIImage? {

    guard let identifier else { return }

    let directory = directoryForID(identifier)
    if let img = imgInCache.objectForKey(identifier) as? UIImage {
        return img
    }
    if let img = NSData(contentsOfFile: directory) {
        return UIImage(data: img)
    }
    return nil
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么一旦为 ViewController 创建了 init,self 就会变成 nil? - Why does self become nil once creating init for a ViewController? 为什么在tableview快速滚动后从coredata返回的数据变为nil? - Why does data returned from coredata become nil after tableview scroll in swift? 为什么self.splitViewController在viewDidLoad中突然为零? - Why is self.splitViewController suddenly nil in viewDidLoad? SwiftUI - 为什么这个选择器的值变成“x – 2”? - SwiftUI - Why does this picker's value become “x – 2”? 为什么引用集合视图中存在的项目会返回 nil 值? - Why does referencing an item that exists in a collection view return a nil value? 为什么snapshot.value作为字典返回nil? - Why does snapshot.value as dictionary return nil? 为什么在展开Optional值时此UIAlertController找不到nil? - why does this UIAlertController fail to find nil while unwrapping an Optional value? 为什么分配的值​​为nil? - Why the assigned value is nil? 为什么执行 segue 后我的 properties 和 outlets 变为 nil? - Why do my properties and outlets become nil after performing a segue? Swift 5:为什么我的结构中的数据突然为空/为零? - Swift 5: Why is my data within struct suddenly empty / nil?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM