tags:

views:

67

answers:

2

I have a class with an instance variable of type SEL, which is a reference to a selector. Inside encodeWithCoder/initWithCoder, how do I encode/decode this type of variable?

+6  A: 

You can covert it to/from a string with NSStringFromSelector and NSSelectorFromString. Selectors are actually strings under the hood anyway.

Tom Dalling
Tom's right that they're strings internally, but don't try to take advantage of that implementation detail. Use the functions he named if you want to use a selector as a string or vice-versa.
Chuck
+3  A: 

You can use encodeValueOfObjCType:at::

SEL s = ...;
[coder encodeValueOfObjCType:@encode(SEL) at:&s];

You can also use NSValue for key archiving.

Or simply you can use NSStringFromSelector and NSSelectorFromString.

notnoop
which one would be the best as far as performance? I need to encode quite a lot of these objects.
Steph Thirion
you need to do some profiling. I think that you are better off using `NSStringFromSelector` because the API is easier to use and get right.
notnoop