iphone - Override functions for a class instance -
i'm developing iphone app , able create similar method 1 android's java:
new getdata(this) { @override protected void onprogressupdate(string... string) { ((loginactivity) context).logincheck(string[0]); } }.execute(data);
here override onprogressupdate function each instance able use result server in different way every instance of getdata.
in objective c code have following code:
getdata *mygetdata = [getdata alloc]; [mygetdata initwithvalue:@"email=blabla&password=blabla&login=blabla"];
how can override functions class instance mygetdata here?
the ordinary way override functions in obj c doesn't seem work in case.
i able override didreceivedata:(nsdata *) data function in each instance
this getdata class now:
@implementation getdata -(id) initwithvalue: (nsstring*) data{ self = [super init]; // create request. nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@"http://myserver.php"]]; [request sethttpmethod:@"post"]; // nsstring *content = @"uid=273&facebook=1&getusernotifications=true"; [request sethttpbody:[data datausingencoding:nsutf8stringencoding]]; [self print]; // create url connection , fire request nsurlconnection *conn = [[nsurlconnection alloc] initwithrequest:request delegate:self]; return self; } - (void)connection:(nsurlconnection *)connection didreceiveresponse:(nsurlresponse *)response { // response has been received, initialize instance var created // can append data in didreceivedata method // furthermore, method called each time there redirect reinitializing // serves clear nslog(@"didreceiveresponse"); _responsedata = [[nsmutabledata alloc] init]; //nslog(@"%@", _responsedata); } - (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { // append new data instance variable declared nslog(@"didreceivedata"); [_responsedata appenddata:data]; nsstring *strdata = [[nsstring alloc]initwithdata:_responsedata encoding:nsutf8stringencoding]; //nslog(@"%@", strdata); } - (nscachedurlresponse *)connection:(nsurlconnection *)connection willcacheresponse:(nscachedurlresponse*)cachedresponse { // return nil indicate not necessary store cached response connection nslog(@"connectionwillcacheresponse"); return nil; } - (void)connectiondidfinishloading:(nsurlconnection *)connection { // request complete , data has been received // can parse stuff in instance variable nslog(@"connectiondidfinishloading"); } - (void)connection:(nsurlconnection *)connection didfailwitherror:(nserror *)error { // request has failed reason! // check error var nslog(@"didfailwitherror"); } @end
consider code:
typedef void (^data_handler_t) (nsurlconnection*connection, nsdata*data); data_handler_t handler = ^(nsurlconnection*connection, nsdata*data){ nslog(@"%@",data); }; handler(nil,[nsdata new]);
then define connection:didreceivedata:
as
- (void)connection:(nsurlconnection *)connection didreceivedata:(nsdata *)data { handler(connection, data); }
pass block in constructor:
@implementation getdata { data_handler_t _handler; } -(id) initwithhandler:(data_handler_t) handler { _handler = handler; // ... }
and call like
getdata *getdata = [[getdata alloc] initwithhandler:^(nsurlconnection*connection, nsdata*data){ nslog(@"%@",data); }];
Comments
Post a Comment