xcode 为什么我不能将 [a objectAtIndex:indexPath.row] 放在 NSString 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5438346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:39:24  来源:igfitidea点击:

Why I can't put [a objectAtIndex:indexPath.row] at NSString

xcodeuitableview

提问by Ruslan

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedFriend = [friends objectAtIndex:indexPath.row];
//selectedFriend invalid summary
//Initialize the detail view controller and display it.
LeaveMViewController *LMController = [[LeaveMViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
LMController.whosmess = selectedFriend;
[LMController release];
LMController = nil;

please help

请帮忙

NSMutableArray *friends;
NSMutableDictionary *listofFriends;


 - (void) parseXMLFileAtURL:(NSString *) URL{   
friends = [[NSMutableArray alloc] init];

NSURL *xmlURL = [NSURL URLWithString:URL];  
friendsParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[friendsParser setDelegate:self];
[friendsParser setShouldProcessNamespaces:NO];
[friendsParser setShouldReportNamespacePrefixes:NO];
[friendsParser setShouldResolveExternalEntities:NO];

[friendsParser parse];

}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download story feed        from web site (Error code %i )", [parseError code]];
NSLog(@"error parsing XML: %@", errorString);

UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
   }

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{          
if ([elementName isEqualToString:@"Friend"]){
    listOfFriends = [[NSMutableDictionary alloc] init];
    [listOfFriends setObject:[attributeDict objectForKey:@"firstname"] forKey:@"firstname"];
    [listOfFriends setObject:[attributeDict objectForKey:@"user_id"] forKey:@"user_id"];        
    [friends addObject:[listOfFriends copy]];   
}

    }
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

// Set up the cell
int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
[cell setText:[NSString stringWithFormat:@"%@, %@",[[friends objectAtIndex: storyIndex] objectForKey: @"firstname"],[[friends objectAtIndex: storyIndex] objectForKey: @"user_id"]]];
return cell;
    }

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedFriend = @"hui";
who=[[NSString alloc] initWithFormat:@"ai bliat %&",[friends objectAtIndex:indexPath.row]];
// NSString *selectedFriend = [friends objectAtIndex:indexPath.row];
LeaveMViewController *LMController = [[LeaveMViewController alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
LMController.whosmess = selectedFriend;
[LMController release];
LMController = nil;

}

回答by Ruslan

problem solved

问题解决了

NSString *selectedFriend = [friends objectAtIndex:indexPath.row];

was replaced by

被替换了

int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
NSString *selectedFriend =[NSString initWithFormat @"%@", [[friends objectAtIndex: storyIndex] objectForKey: @"firstname"]];