【问题标题】:NSPredicate check for kind of object classNSPredicate 检查对象类的种类
【发布时间】:2011-11-09 13:41:51
【问题描述】:

我有一个 UIView 对象数组。我想在这个数组上调用- (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate 来获取MyCustomView 对象的数组。

如何用“isKindOf:”编写谓词?

【问题讨论】:

    标签: iphone objective-c ios nspredicate


    【解决方案1】:

    尝试(已弃用

    [NSPredicate predicateWithFormat: @"className == %@", [someObject className]]
    

    或者

    [NSPredicate predicateWithFormat: @"class == %@", [someObject class]]
    

    【讨论】:

    • 我也可以和className一起使用。虽然class 看起来更干净。
    • 哪个更合适? “=”还是“喜欢”?
    • 使用like时,?*可以作为通配符使用。 developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/…
    • 这个谓词肯定等同于isMemberOfClass:,而不是原始问题的isKindOfClass:
    • 正如@Tommy 所说,这不是所述问题的正确答案。下面zekel给出了正确答案。
    【解决方案2】:

    我在使用 Jef 的方法时遇到了错误。不过,这对我有用。

    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                                                  @"self isKindOfClass: %@", class];
    

    来源:https://stackoverflow.com/a/2556306/168594

    【讨论】:

      【解决方案3】:

      对于任何感兴趣的人,这里是 Jef 方法的 Swift 版本:

      NSPredicate(format: "className = %@", String(describing: MyCustomView.self))
      

      【讨论】:

        【解决方案4】:

        使用-className 作为您的密钥怎么样?

        NSPredicate* foo = [NSPredicate predicateWithFormat: @"className == %@", @"MyCustomView"];
        

        【讨论】:

          猜你喜欢
          • 2023-03-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-29
          • 2011-09-28
          • 1970-01-01
          • 2017-05-25
          • 1970-01-01
          相关资源
          最近更新 更多