简体   繁体   English

如何获取CIFilter的显示名称?

[英]How to get the displayname of a CIFilter?

I'm trying to get a list of the display names of the CIFilters. 我正在尝试获取CIFilter的显示名称的列表。 Have found this site which has the constants that should give the name (with other info), but the code below is not working. 找到了该站点该站点具有应给出名称的常量(带有其他信息),但是下面的代码不起作用。

Console output: 控制台输出:

here 01
here 02
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CIAccordionFoldTransition 0x7a672dc0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key CIAttributeFilterName.'

My code: 我的代码:

func getCIFilterName(filterName: String) -> String{
    var res = "abc"

    let fltr = CIFilter(name:filterName)
    println("here 01")
    if contains(fltr.attributes().keys, kCIAttributeFilterDisplayName){
        println("here 02")
        res = fltr.valueForKey(kCIAttributeFilterDisplayName) as String
    }
    println("here 03")

    return res
}

The attributes method returns a dictionary. attributes方法返回一个字典。 So much simpler to do it like this: 这样简单得多:

func getCIFilterName(filterName: String) -> String {
    var res = "abc"
    let fltr = CIFilter(name:filterName)
    if let disp = fltr.attributes()[kCIAttributeFilterDisplayName] as? String {
        res = disp
    }
    return res
}

But I do not like your trick of returning a false value "abc" if we fail. 但是,我不喜欢您在失败时返回错误值"abc"技巧。 This is what Optionals are for - to get us away from "magic values" of this sort. 这就是Optionals的目的-使我们摆脱这种“魔术值”。 So rewrite like this: 所以像这样重写:

func getCIFilterName(filterName: String) -> String! {
    var res : String! = nil
    let fltr = CIFilter(name:filterName)
    if let disp = fltr.attributes()[kCIAttributeFilterDisplayName] as? String {
        res = disp
    }
    return res
}

Do not forget, however, to check the returned value against nil, or you'll crash if you try to use it for anything (if it is nil). 但是,请不要忘记将返回值与nil进行比较,否则如果尝试将其用于任何内容(如果 nil),则可能会崩溃。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM