【问题标题】:Multiple methods named "setLength" found找到多个名为“setLength”的方法
【发布时间】:2013-08-07 23:48:43
【问题描述】:

我有这个代码:

[[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”的方法。”

关于我应该如何解决它的任何想法?

【问题讨论】:

  • 嗨,你能告诉 self.receivedData 是由什么组成的吗?我的意思是它包含什么?并确保您已为相应的连接对象提供标签。

标签: ios xcode methods


【解决方案1】:

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

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

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

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

【讨论】:

  • 或者,如果一个人真的嫁给了长长的连环杀手陈述,请使用适当的演员表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 2021-12-09
  • 2015-05-27
  • 2019-12-28
相关资源
最近更新 更多