iphone - Why do we call doesNotRecognizeSelector: method? -
i working socket programming.i wanted clear doubt related code downloaded -mobileorchard.com - chatty. while r&d , saw function call in chatroomviewcontroller.m file
[chatroom broadcastchatmessage:input.text fromuser:[appconfig getinstance].name];
when saw in room.m file, implementation of above call;
- (void)broadcastchatmessage:(nsstring*)message fromuser:(nsstring*)name { // crude way emulate "abstract" class [self doesnotrecognizeselector:_cmd]; }
i googled "doesnotrecognizeselector:" , according apple error handling, stating "the runtime system invokes method whenever object receives aselector message can’t respond or forward." question why developer call broadcastchatmessage:fromuser: function if none of use there , handle method's "selector not found" exception ?
according stackovrflow, used create abstract class , according question, avoid "incomplete implementation" warning.
i still not getting why method used in chatty code, kindly me understand reason why method used.
this method exists on every nsobject
derived object triggers path exception when method isn't recognized in runtime call. example, if try send message nsstring
called -foo
, it'll end there since that's not valid method on nsstring
.
in case, chatty class room
base class never used directly. localroom
, remoteroom
derive it, , both of classes provide overriding implementation of -broadcastchatmessage:fromuser
. nobody ever calls base class version, "completeness" programmer has guaranteed subclasser must override implementing method, turning around , calling trigger exception.
thing is, isn't idiomatic objective-c. "abstract" class concept c++ , other languages; it's base class exists "pattern" subclass. (in objc, done creating formal @protocol
when there isn't meaningful state, there (mostly) isn't here).
note call -doesnotrecognizeselector:
arbitrary. it's not necessary avoid compiler warnings here (since method in fact implemented) , original writer have thrown exception directly, or done nothing instead.
Comments
Post a Comment