Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IOS正在为NSURL创建实用程序类_Ios_Objective C_Nsurlconnection - Fatal编程技术网

IOS正在为NSURL创建实用程序类

IOS正在为NSURL创建实用程序类,ios,objective-c,nsurlconnection,Ios,Objective C,Nsurlconnection,我有一个项目,我正在工作,有许多不同的新闻源和公告板,显示来自不同来源的帖子。目前,对于显示提要的视图,我在每个类文件中包含的方法中有like、delete和flag按钮的代码。我一直在尝试创建一个实用程序类,它允许我将上面列出的三个功能的代码放在一个对象中,以便在整个项目中使用。在C++或java中,我做了完全相同的事情,但是我在Objtovi-C中有问题。like、delete和flag按钮使用NSURL库与web服务交互。下面是我试图在实用程序类中实现的方法之一的示例,以及用于在类似按钮中

我有一个项目,我正在工作,有许多不同的新闻源和公告板,显示来自不同来源的帖子。目前,对于显示提要的视图,我在每个类文件中包含的方法中有like、delete和flag按钮的代码。我一直在尝试创建一个实用程序类,它允许我将上面列出的三个功能的代码放在一个对象中,以便在整个项目中使用。在C++或java中,我做了完全相同的事情,但是我在Objtovi-C中有问题。like、delete和flag按钮使用NSURL库与web服务交互。下面是我试图在实用程序类中实现的方法之一的示例,以及用于在类似按钮中实现的代码:

+ (void)btnLikeAction:(UIButton *)btnLike userIdString:(NSString *)userId contentSource:(NSString *)sourceId cellUsed:(NewsfeedCell *)cell dataForCell:(Post *)post
{
    BOOL hasLiked = post.hasLiked;
    UILabel *likeLabel = cell.likeCount;
    NSString *pId = post.postId;

    if (hasLiked)
    {
        [btnLike setEnabled:NO];

        NSString *url = [NSString stringWithFormat: @"http://192.155.94.183/api/v1/likes/unlike/%@/%@/%@/", sourceId, pId, userId];

        NSURL *URL = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];

        int localCount = [likeLabel.text intValue];
        localCount--;
        likeLabel.text = [NSString stringWithFormat:@"%i", localCount];
        post.likeCount = [NSString stringWithFormat:@"%i", localCount];
        post.hasLiked = NO;

        [btnLike setEnabled:YES];
    }
    else
    {
        [btnLike setEnabled:NO];

        NSError *err = nil;
        NSDictionary *likeData = [NSDictionary dictionaryWithObjectsAndKeys:
                                  userId, @"user_id",
                                  pId, @"source_id",
                                  sourceId, @"source",
                                  nil];

        NSData *JSONLike = [NSJSONSerialization dataWithJSONObject:likeData options:NSJSONWritingPrettyPrinted error:&err];
        NSString *url = @"http://192.155.94.183/api/v1/likes.json";

        NSURL *URL = [NSURL URLWithString:url];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL
                                                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                           timeoutInterval:30.0];

        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:[NSString stringWithFormat:@"%d", [JSONLike length]] forHTTPHeaderField:@"Content-Length"];
        [request setHTTPBody:JSONLike];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        [connection start];

        int localCount = [likeLabel.text intValue];
        localCount++;
        likeLabel.text = [NSString stringWithFormat:@"%i", localCount];
        post.likeCount = [NSString stringWithFormat:@"%i", localCount];
        post.hasLiked = YES;

        [btnLike setEnabled:YES];
    }
}

这段代码使用web服务来更新特定内容的赞数。当方法被放置到各个ViewController类文件中时,它可以工作,但当我尝试使用各个方法创建实用程序类时,我遇到了与
didReceiveAuthenticationChallenge
didReceiveResponse
相关的问题,
didReceiveData
ConnectiondFinishLoading
方法未被调用。最初,我假设将在调用实用程序方法的文件中调用委托方法。但事实并非如此。当我在实际的实用程序类中实现方法定义时,这些方法仍然没有被调用。我做了一些关于这个主题的研究,并进行了调查,但发现我无法找到有助于我的具体情况的大量资源。如何设置我的实用程序类?如果需要,我可以发布实用程序的完整代码。

正如@serrrgi已经说过的,问题是
btnLikeAction:…
是一个类方法,因此
self
就是类本身。您有以下选项:

  • 使所有委托方法成为类方法,例如

    + (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        NSLog(@"didReceiveResponse");
    }
    
  • 创建实用程序类的实例并将其用作委托:

    YourClass *u = [[self alloc] init];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:u];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (data != nil) {
            NSLog(@"success");
        } else {
            NSLog(@"error: %@", error);
        }
    }];
    
  • 使用不需要委托的
    sendAsynchronousRequest:…

    YourClass *u = [[self alloc] init];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:u];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (data != nil) {
            NSLog(@"success");
        } else {
            NSLog(@"error: %@", error);
        }
    }];
    

您的自定义类是否符合NSURLConnectionLegate?由于您是从静态方法将NSURLConnection委托设置为“self”,因此您将自定义类作为委托传递,而不是传递自定义类的实例。将你的类方法转换为实例方法,它应该可以工作。不,我不这么认为,我目前正在阅读NSURL文档。如何使其符合NSURLConnectionLegate@serrrgi@ScottOBot:serrrgi是对的。或者,您可以使用不使用委托的
sendAsynchronousRequest:queue:completionHandler:
方法。我正在实现上述解决方案,如果我成功使用flag和delete,您将知道我是否成功!非常感谢你,伙计@马丁R