开发者

How does NSValue do its magic?

I have an MVC application. The开发者_开发百科 model has a property that is a struct NSSize. It is writable like this:

- (void)setSize:(NSSize)aSize;

The view sets this NSSize using key-value-coding. However, you can not key-value-code a struct, so I wrapped it in an NSValue-object like this:

[theView setValue:[NSValue valueWithSize:mySize]
           forKey:@"theModel.size"];

To my understanding, this should not work since the accessor expects a struct and not an NSValue. But it works perfectly. Magically.

How is this possible?


Scroll down to "Wrapping and Unwrapping Structs", but here's the gist:

setValue:forKey: determines the data type required by the appropriate accessor or instance variable for the specified key. If the data type is not an object, then the value is extracted from the passed object using the appropriate -<type>Value method.

Btw, thanks for asking this question! This is one of those cocoa things that has always "just worked" and I never considered that it wasn't obvious how it was accomplished.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜