找到了名为“setLength”的多个方法

时间:2013-08-07 23:48:43

标签: ios xcode methods

我有这段代码:

[[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“found。”

关于我应该采取什么措施来解决它?

1 个答案:

答案 0 :(得分:2)

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

BTW - 使用现代语法:

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

@(connection.tag)取代[NSNumber numberWithInt:connection.tag]的使用,self.receivedData[xxx]取代objectForKey:的使用。

相关问题