gpt4 book ai didi

objective-c - 是否可以将 "keyed-subscripting"添加到 Class 对象?

转载 作者:搜寻专家 更新时间:2023-10-30 19:43:51 24 4
gpt4 key购买 nike

在......的脉络中

@implementation MyClass
- (id) objectForKeyedSubscript:(id)k {
return [self something:k];
}

是否也可以“下标”Class 对象?我也将要和你一起找出答案......但我想我会在测试时发布这个问题,我自己......

+ (id) objectForKeyedSubscript:(id)k { 
return [self.shared something:k];
}

唉..它不是...

id x = MyClass[@"document"];

error: unexpected interface name 'MyClass': expected expression

但为什么,爸爸? Class' sure get the short end NSObject 的棍子,如果你问我的话

最佳答案

Josh Caswell's comment指出问题:

You're using the type name. Try it with a class object in a variable. id myClass = [MyClass class]; myClass[@"document"]; Or [MyClass class][@"document"]

我发誓我已经尝试过了圣经。但证据就在布丁中......

@interface MyClass : NSObject
@property (readonly) id boring;
@end
@implementation MyClass
- boring { return @"typical"; }
+ document { return @"YEEHAW"; }
- objectForKeyedSubscript:key { return [self valueForKey:key]; }
+ objectForKeyedSubscript:key {
return [self performSelector:NSSelectorFromString(key)];
}
@end

...

id a = MyClass.new;
id x = a[@"boring"]; // "typical" (via "normal" keyed subscription)
id b = MyClass.class;
x = z[@"document"]; // "YEEHAW" (via CLASS keyed-subscript!)

或者我所有的单行freaky-deaky's 在那里...

x = ((id)MyClass.class)[@"document"] // "YEEHAW" 

关于objective-c - 是否可以将 "keyed-subscripting"添加到 Class 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23440600/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com