gpt4 book ai didi

iPhone键值编码——测试键是否存在

转载 作者:行者123 更新时间:2023-12-03 18:39:50 25 4
gpt4 key购买 nike

iPhone 键值编码是否有办法测试某个类是否接受给定的键?

也就是说,在不实现目标类的 valueForUndefinedKey: 或 setValue: forUndefinedKey: 方法的情况下,我希望能够执行以下操作:

if ([targetObject knowsAboutKey: key])  {
[targetObject setValue: value forKey: key];
}

最佳答案

setValue:forUndefinedKey: 的默认实现会引发 NSUndefinedKeyException。您可以将尝试包含在 try/catch block 中:

@try{
[targetObject setValue:value forKey:key];
} @catch (NSException *e) {
if ([[e name] isEqualToString:NSUndefinedKeyException]) {
NSLog(@"oh well... handle the case where it has no key here."); // handle
} else {
[[NSException exceptionWithName:[e name]
reason:[e reason]
userInfo:[e userInfo]]
raise];
}
}

关于iPhone键值编码——测试键是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3892227/

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