ios - Allocate any mutable object with immutable class or vice versa -
this may sound pretty strange people.. went on ask this.. obviously, no 1 wants it.. make concept clearer, want ask. this:
nsstring *mystring=[[nsstring alloc]init]; nsstring *mystring=[nsstring string];
as far understand, gives pointer object of nsstring
class, but, if this:
nsstring *mystring=[[nsmutablestring alloc]init]; nsstring *mystring=[nsmutablestring string];
if happens, kind of class "string" belong.. , since have initialized mutable class, can i send messages of nsmutablestring class "mystring", object of nsstring class ?? whatever case can know concept behind this.. also, can in case of arrays, dictionaries , many other calsses.
yes. type of object 1 used alloc method. if allocated nsmutablestring, object member of nsmutablestring class.
nsstring *mystring=[[nsmutablestring alloc]init];
what's going on it's have pointer allocated object type of it's parent class, compiler not see methods of nsmutablestring , warning if try call them directly.
but again, object it's still member of nsmutablestring class, , respond messages of nsmutablestring class.
even though declared mystring pointer nsstring, can perform test see i'm talking:
bool test = [mystring iskindofclass:[nsmutablestring class]]; //this hold true
you can perform cast method call explicit.
nsstring *mystring=[[nsmutablestring alloc]init]; [(nsmutablestring*)mystring somensmutablestringspecificmethod];
ps: counts not mutable / immutable objects, specialization classes
Comments
Post a Comment