objective c - Decode Class from @encoded type string -


objective-c's @encode produces c strings represent type, including primitives, , classes, so:

nslog(@"%s", @encode(int));       // nslog(@"%s", @encode(float));     // f nslog(@"%s", @encode(cgrect));    // {cgrect={cgpoint=ff}{cgsize=ff}} nslog(@"%s", @encode(nsstring));  // {nsstring=#} nslog(@"%s", @encode(uiview));    // {uiview=#@@@@fi@@i{?=b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b6b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b3b1b1b1b2b2b1}} 

so, can meaningful encoding of class (one contains class name) using @encode(classname), it's in same format encoding of generic struct (as in above example).

now, question is, given any (valid of course) type encoding, possible find out whether encoding of objective-c class, , if so, class object corresponds encoding?

of course, could try parse class name out of type encoding, , class using nsclassfromstring, doesn't sound right way it, or particularly performance-efficient. best way achieve this?

unfortunately, not think there way sure working class using encoding.

dasblinkenlight has idea in comments, although didn't support other instance variables. full format class {classname=#ivars}. however, potentially catch other structures well, explain below.

the reason classes encoded treated structures @encode. { indicates start of structure, , followed structure's name , equals sign, contents, , }. reason classes have # after equals sign first element in class structure of type class, , how type encoded. means any structure beginning class match format. if there instance variables listed, encoded after #. example, here reconstruction of uiview using encoding posted.

structure                         encoding  struct uiview {                   {uiview=     class class1;                  #     id id1, id2, id3, id4;         @@@@  object pointers encode id.     float float1;                  f     int int1;                          id id5, id6;                   @@     unsigned int uint1;                struct /*anonymous*/ {         {?=         unsigned bitfield1 : 1;     b1   type of bitfield not encoded.         unsigned bitfield2 : 1;     b1   chose unsigned.         ...         unsigned bitfield15 : 1;    b1         unsigned bitfield16 : 6;    b6         unsigned bitfield17 : 1;    b1         ...         unsigned bitfield58 : 1;    b1         unsigned bitfield59 : 3;    b3         unsigned bitfield60 : 1;    b1         unsigned bitfield61 : 1;    b1         unsigned bitfield62 : 1;    b1         unsigned bitfield63 : 2;    b2         unsigned bitfield64 : 2;    b2         unsigned bitfield65 : 1;    b1     };                             } }                                 } 

(decoded using _c_* constants in /usr/include/objc/runtime.h)

if put structure in code (filling in ...s), both @encode(uiview) , @encode(struct uiview) give same result. interestingly, beginning structure class technically make valid class type, , send messages pointer one. beware, however, instance variables defined in class interface placed in encoding, locations of others determined @ runtime, taking advantage of create own objects not advised.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -