English 中文(简体)
动态设置目标- c iphone 中键值编码的密钥
原标题:dynamically set the key for Key-Value Coding in objective-c iphone

我有一个班叫回召:

[mycallback setValue:[code objectForKey:@"abc"] forKey:@"abc"];

它被调用了很多我的控制器。 THE 是指我从 Mysql 数据库调出数据,然后发送到我的调回处。 所以我可能不知道键是什么。 除此之外, 我不想在我的调回类中声明实例变量, 因为我想要分离它。

不幸的是,如果上面的代码, 我得到这个信息:

exception  NSUnknownKeyException , reason:  [<CallbackClass 0x7b6c5d0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key fence. 

所以我想动态地设置键, 而不是在我的回召中。 所以我为 NS 调用创建了一个分类, 并添加值For For UnfinedKey 方法 :

- (id)valueForUndefinedKey:(NSString *)key
{
    NSLog(@"This key not being called %@", key);
    return nil;
}

1) 值未调用未定义的密钥, 2) 我不清楚该放在哪里才能让错误消失 。

感谢答复。

问题回答

您定义了 valuefForUnfinedKey: 是返回值的处理方式。 但在您的代码片断中,您似乎正在试图为未定义的密钥设定值。 在这种情况下,使用的方法是 :

setValue:forUndefinedKey:

举例来说,基于你看起来想做的事:

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {
    NSLog(@"Attempting to set value: %@ for key: %@", value, key);
}




相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签