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
2009-07-16 03:31:33
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
2009-07-16 03:44:26
+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
2009-07-16 03:56:53
which one would be the best as far as performance? I need to encode quite a lot of these objects.
Steph Thirion
2009-07-16 19:35:32
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
2009-07-16 19:50:39