你能简化这个代码吗?

时间:2011-05-24 18:21:14

标签: objective-c uitableview

下面的代码执行我需要它做的事情,但它看起来像火车残骸并且仍然在输出周围添加引号。我猜想可以在三到五行中得到相同的结果,并且可以免费引用。每次这样做时,我真的必须删除括号和空格吗?请建议更正?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    NSString *key = [terms objectAtIndex:row];
    NSArray *nameSection = [letters objectForKey:key];
    NSString *one = @"()";
    NSString *two = [NSString stringWithFormat:@"%@", nameSection];
    NSString *four = [two stringByTrimmingCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:one]];
    NSString *five = [four stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]];
    NSString * definition = [[NSString alloc] initWithFormat:@"%@", five];
    def.text = definition;
    [definition release];
}  

3 个答案:

答案 0 :(得分:0)

编辑:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *key = [terms objectAtIndex:indexPath.row];
    NSString *nameSection = [NSString stringWithFormat:@"%@", [letters objectForKey:key]];
    NSString *trimmedString = [nameSection stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"()\"\n\r"]];

    def.text = trimmedString;
}

您可以将\"添加到one以使报价消失。并且可以跳过definition行,您可以改为def.text = five;

答案 1 :(得分:0)

这是第二遍:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    NSString *key = [terms objectAtIndex:row];
    NSArray *nameSection = [letters objectForKey:key];

    NSMutableCharacterSet charSet = [NSMutableCharacterSet characterSetWithCharactersInString:@"()"];
    [charSet formUnionWithCharacterSet:[NSCharacterSet newlineCharacterSet]];

    def.txt = [[NSString stringWithFormat:@"%@", nameSection] stringByTrimmingCharactersInSet:charSet];
}  

答案 2 :(得分:0)

当然,twodefinition字符串已过时,因为它们是nameSectionfive的相同副本。