在方法中使用类似代码时的最佳实践

时间:2012-04-24 08:51:48

标签: objective-c coding-style

我有一个Utility方法来与API通信,它使用POST HTTP请求进行通信,在我的实用程序类中,我有一个名为的方法:

(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters;

获取参数并设置POST的主体。但是,在一个特定的情况下,我想上传一些图像,并稍微改变代码,使上传图像成为可能,这种情况的最佳做法是什么?我应该将方法重命名为:

(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;

并在所有其他情况下将nil设置为参数,或者我应该在调用“makeConnectionWithParameters”的方法中设置bool并检查方法是否设置了bool并且在这种情况下处理图像?

使代码更漂亮的任何其他想法?

这是方法:

(void)makeConnectionWithParameters:(NSMutableDictionary*)parameters
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    NSURL *url = [NSURL URLWithString:BASE_URL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20];
    request.HTTPMethod = @"POST";

    NSString *boundary = @"myR4ND0Mboundary";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    //Lägg till inloggningsuppgifter för API-anropet
    [parameters setValue:API_LOGIN forKey:@"api-login"];
    [parameters setValue:API_PASSWORD forKey:@"api-password"];

    //Lägg till alla parameterar i POST-bodyn
    NSMutableData *body = [NSMutableData data];

    for (NSString *param in parameters) 
    {
        [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@\r\n", [parameters objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    if(hasImages)
    {
        int c = 0;
        for(UIImage* image in self.images)
        {
            c++;
            NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
            if (imageData) 
            {
                [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n", [NSString stringWithFormat:@"image%d", c]] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:[[NSString stringWithString:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                [body appendData:imageData];
                [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
            }
            imageData = nil;
        }
    }

    //Sätt content-length
    NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:body];
    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(connection)
    {
        receivedData = [NSMutableData dataWithLength:0];
    }
}

我希望这一切都有道理。 :)

1 个答案:

答案 0 :(得分:1)

只需创建两个创建请求对象的方法,而不是实际的连接:

(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters;
(NSMutableURLRequest*)makeConnectionWithParameters:(NSMutableDictionary*)parameters andImages(NSArray*)images;

在第一个方法调用内:

[self makeConnectionWithParameters:parameters andImages:nil];

...并且对第二种方法中的图像进行零检查。使用这两种方法获取您的请求,然后使用返回的对象进行NSURLConnection。