iphone - 覆盖类实例的函数

标签 iphone ios objective-c

我正在开发一个 iPhone 应用程序,并希望能够从 Android 的 Java 创建一个与此类似的方法:

new GetData(this) {

        @Override
        protected void onProgressUpdate(String... string) {
            ((LoginActivity) context).loginCheck(string[0]);
        }
    }.execute(data);

在这里,我为每个实例覆盖了 onProgressUpdate 函数,以便能够以不同的方式为我的 GetData 的每个实例使用来自服务器的结果。

在我的 Objective C 代码中,我有以下代码:

GetData *myGetData = [GetData alloc];
[myGetData initWithValue:@"email=blabla&password=blabla&login=blabla"];

如何在此处为我的类实例 myGetData 覆盖某些函数?

在这种情况下,覆盖 Obj C 中函数的普通方法似乎不起作用。

我希望能够在每个实例中覆盖 didReceiveData:(NSData *) 数据函数

现在这是我的 GetData 类:

@implementation GetData
-(id) initWithValue: (NSString*) data{
self = [super init];
// Create the 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 and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
return self;
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// A response has been received, this is where we initialize the instance var you created
// so that we can append data to it in the didReceiveData method
// Furthermore, this method is called each time there is a redirect so reinitializing it
// also serves to clear it
NSLog(@"didReceiveResponse");
_responseData = [[NSMutableData alloc] init];
//NSLog(@"%@", _responseData);
}
 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to the instance variable you 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 to indicate not necessary to store a cached response for this connection
NSLog(@"connectionwillCacheResponse");
return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// The request is complete and data has been received
// You can parse the stuff in your instance variable now
NSLog(@"connectionDidFinishLoading");

}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// The request has failed for some reason!
// Check the error var
NSLog(@"didFailWithError");
}
@end

最佳答案

考虑这段代码:

typedef void (^data_handler_t) (NSURLConnection*connection, NSData*data);

data_handler_t handler = ^(NSURLConnection*connection, NSData*data){
    NSLog(@"%@",data);
};

handler(nil,[NSData new]);

然后定义connection:didReceiveData:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    handler(connection, data);
}

在构造函数中传递 block :

@implementation GetData {
    data_handler_t _handler;
}

-(id) initWithHandler:(data_handler_t) handler {
    _handler = handler;
    // ...
}

并称它为

GetData *getData = [[GetData alloc] initWithHandler:^(NSURLConnection*connection, NSData*data){
    NSLog(@"%@",data);
}];

关于iphone - 覆盖类实例的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19223897/

相关文章:

iphone - 使用 sqlite3_step(statement) 进行不一致但频繁的 EXC_BAD_ACCESS 访问

ios - 覆盖 touchesBegan : Error - method does not override any method from superclass

ios - 将表格滚动到底部后用新数据更新 UITableView 的数据源

iphone - NSString到NSArray

iphone - 如何在 UItextView 中添加 placeHolder 文本?在 iPhone SDK 中

ios - Storyboard中带有不同颜色文本的 UILabel

ios - Storyboard 上的 UIToolbar 和 UIBarButtonItem 行为,工具栏似乎掩盖了按钮项

iphone - 将 NSArray 写回 plist

ios - TableViewCell 中的更改立即生效

IOS - 当应用程序在后台从 UNNotificationAction 触发后发送 http 请求