ios - Need assistance regarding few concepts of objective-c protocols -
1> there other way use/avail/consume protocol without doing @interface myclass : nsobject <someprotocol>
.
2> can class become delegate of delegating class without doing @interface myclass : nsobject <someprotocol>
.
3> right regarding these
id<aprotocol> *myvar1;
means myvar1
hold object of class class must implement <aprotocol>
?
aclass<bprotocol> *myvar2;
means myvar2
hold object of aclass
, must implement <bprotocol>
?
4> doing (myclass <someprotocol> *)[[myclass alloc] init];
, myclass
in interface not inheriting <someprotocol>
.
yes, long object implements required interface of protocol object can stand in object explicitly conforms protocol. if loose compiler helping out confirming implement required methods. it's best explicit , declare class conforms protocol.
when as long object implements required interface of protocol
mean object informally conforms in sense implements methods required not create explicit trying conform. e.g. object valid uitableviewdatasource
implements @required
methods, not declare such.
@interface myobject : nsobject - (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section; - (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath; @end
again should create these relationships clear , conform @interface myobject : nsobject <uitableviewdatasource>
a)id<aprotocol> *myvar1;
should written without asterisk id<aprotocol> myvar1;
, saying have variable called myvar1
should conform aprotocol
- lie (see reply 3) b) right except it must implement <bprotocol>
strong, saying have variable called myvar2
, should conforms bprotocol
- 1 time again lie (see reply 3)
here performing cast homecoming type of method myclass
myclass<someprotocol>
. saying compiler "i know object returns object of type myclass
telling myclass<someprotocol>
". if myclass
not defined @interface myclass : nsobject <someprotocol>
lying compiler , there chance introduce bugs compiler cannot tell about.
you should careful casting things quiet compiler (the compiler clever) illustration compile code without issues crash @ runtime
uiview *view = (id)@"hey i'm not view"; view.frame = cgrectzero;
ios objective-c protocols
No comments:
Post a Comment