1

我有这个代码:

[[self.receivedData objectForKey:[NSNumber numberWithInt:connection.tag]] setLength:0];

并且重复了三遍

#pragma mark MyURLConnection delegate methods
- (void)connection:(MyURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"Response received...");

    [[self.receivedData objectForKey:[NSNumber numberWithInt:connection.tag]] setLength:0];
}

- (void)connection:(MyURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"Receiving data...");
    [[self.receivedData objectForKey:[NSNumber numberWithInt:connection.tag]] appendData:data];
}
- (void)connectionDidFinishLoading:(MyURLConnection *)connection {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
    NSLog(@"Request Succeeded! Received %d bytes of data",[[self.receivedData objectForKey:[NSNumber numberWithInt:connection.tag]] length]);

    //do something with data
    NSString *response = [[NSString alloc] initWithData:[self.receivedData objectForKey:[NSNumber numberWithInt:connection.tag]] encoding:NSASCIIStringEncoding];
    NSLog(@"HTTP Response: %@",response);
    if(connection.tag == APNRegisterURLConnection) {
        if([response isEqualToString:kPushNotificationRegistrationSuccess])
            [self.userNotifications setObject:[NSNumber numberWithBool:TRUE] forKey:kRegistered];
        else if ([response isEqualToString:kPushNotificationReRegistrationSuccess]){
            [self.userNotifications setObject:[NSNumber numberWithBool:TRUE] forKey:kRegistered];
            NSUUID *udid = [[UIDevice currentDevice] identifierForVendor];
            NSString *httpBody = [NSString stringWithFormat:@"udid=%@",udid];
            NSString *url = [kRequestURLAddress stringByAppendingString:@"updatenotifications.php"];
            [self postRequestForURL:url withParameters:httpBody andTag:UpdateNotificationsURLConnection andPassingData:nil andOptionalMethod:@selector(displayUIActivityIndicatorView)];
        }
        else {
            [self alertWithTitle:@"Error configuring push notifications" andMessage:kUnknownErrorAlertMessage];
        }
    }

    else if(connection.tag == AddNotificationURLConnection) {
        if([response isEqualToString:kPushNotificationAdditionSuccess]){
            [[self.userNotifications valueForKey:kNotifications] addObject:connection.passingData];
            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        }
        else {
            [self alertWithTitle:@"Error adding notification" andMessage:kUnknownErrorAlertMessage];
        }
    }
    else if(connection.tag == DeleteNotificationURLConnection) {
        if([response isEqualToString:kPushNotificationDeletionSuccess]){
            [[self.userNotifications valueForKey:kNotifications] removeObjectAtIndex:[connection.passingData row]];
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:connection.passingData] withRowAnimation:UITableViewRowAnimationFade];
        }
        else {
            [self alertWithTitle:@"Error deleting notification" andMessage:kUnknownErrorAlertMessage];
        }
    }
    else if(connection.tag == UpdateNotificationsURLConnection) {
        if(![response isEqualToString:@"error selecting udid"]){
            //load notifications with myurlconnection, since internet is required to add items locally, logical sync is syncing everything to match the database 

            //sample format for list of notifications 
            // (int)periodindex (int)timeindex|(int)periodindex2 (int)timeindex2|(int)periodindex3 (int)timeindex3 (sorted by period index
            // i.e.  --> 0 13|2 6|4 11|4 6|9 4
            NSArray *unparsedNotifications = [response componentsSeparatedByString:kNotificationDelimeter];
            NSMutableArray *parsedNotifications = [NSMutableArray arrayWithCapacity:[unparsedNotifications count]];

            int x;
            if(!([[unparsedNotifications objectAtIndex:0] isEqualToString:@""] && [unparsedNotifications count] == 1)) {//no hits in database
                NSArray *notificationStringArray;
                Notification *parsed;
                for (x=0; x<[unparsedNotifications count]; x++) {
                    notificationStringArray = [[unparsedNotifications objectAtIndex:x] componentsSeparatedByString:kElementDelimeter];

                    parsed = [Notification withPeriod:[[notificationStringArray objectAtIndex:0] intValue] andTime:[[notificationStringArray objectAtIndex:1] intValue]];

                    [parsedNotifications addObject:parsed];
                }
                NSLog(@"number of notifications in database = %d",[parsedNotifications count]);
            }
            //add missing notifications to array
            Notification *value;

            for(x=0;x<[parsedNotifications count];x++){
                value = [parsedNotifications objectAtIndex:x];
                if(![[self.userNotifications valueForKey:kNotifications] containsObject:value]){
                    [[self.userNotifications valueForKey:kNotifications] addObject:value];
                    NSLog(@"Adding notification with period:%@ and time:%@",[value periodString],[value timeString]);
                }
            }

            //delete objects in local array that are missing from online, reversed in order to remove largest indices and rows so that issue does not arise from looping through an array that has been changed


            for(x=[[self.userNotifications valueForKey:kNotifications] count]-1;x>=0;x--){
                value = [[self.userNotifications valueForKey:kNotifications] objectAtIndex:x];
                if(![parsedNotifications containsObject:value]){
                    NSLog(@"Deleting notification with period:%@ and time:%@",[value periodString],[value timeString]);
                    [[self.userNotifications valueForKey:kNotifications] removeObjectAtIndex:x];
                }
            }

            [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
        }
        else
            [self alertWithTitle:kDefaultAlertTitle andMessage:kUnknownErrorAlertMessage];
    }

    [self cleanUpAfterRequest:connection.tag];
    [response release];

    [connection release];
    [[self.receivedData objectForKey:[NSNumber numberWithInt:connection.tag]] setLength:0];
}

-(void)cleanUpAfterRequest:(int)connectionType{
    if(connectionType == AddNotificationURLConnection){
        self.tableView.allowsSelection = TRUE;
        self.tableView.allowsSelectionDuringEditing = TRUE;     
    }
    else if(connectionType == DeleteNotificationURLConnection){
        self.view.userInteractionEnabled = TRUE;
    }

    else if(connectionType == UpdateNotificationsURLConnection){
        [[self.view viewWithTag:5] removeFromSuperview];
    }
}

- (void)connection:(MyURLConnection *)connection didFailWithError:(NSError *)error {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
    NSString *failingKey = nil;
    //IF_PRE_IOS4(failingKey = NSErrorFailingURLStringKey;);
    failingKey = NSURLErrorFailingURLStringErrorKey;

    NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:failingKey]);
    [self cleanUpAfterRequest:connection.tag];
    [self alertWithTitle:kDefaultAlertTitle andMessage:kDefaultAlertMessage];
    [connection release];
    [[self.receivedData objectForKey:[NSNumber numberWithInt:connection.tag]] setLength:0];
}

Xcode 返回错误“找到多个名为“setLength”的方法。”

关于我应该做些什么来解决它的任何想法?

4

1 回答 1

2

将结果分配[self.receivedData objectForKey:[NSNumber numberWithInt:connection.tag]]给适当类型的变量。然后调用该setLength:变量的方法。

顺便说一句 - 使用现代语法:

self.receivedData[@(connection.tag)];

@(connection.tag)替换使用并[NSNumber numberWithInt:connection.tag]替换self.receivedData[xxx]使用objectForKey:

于 2013-08-07T23:56:00.127 回答