你能简化这个代码吗?
下面的代码做我需要它做的,但它看起来像一列火车残骸,仍然在输出周围添加引号。我猜想可以在三到五行中得到相同的结果,并且无需引用。每次我这样做,我真的必须删除括号和空格吗?请建议更正?你能简化这个代码吗?
- (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];
}
编辑:
- (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;
。
我试过“以前没有成功,但是这看起来不错,我会尽快尝试它。” – StreaminJA 2011-05-24 19:03:00
我没有测试过它,但如果stringByTrimmingCharactersInSet不起作用,那么stringByReplacingOccurrencesOfString将与\“一起使用。 – vakio 2011-05-24 19:08:33
它删除了最后一个引号,但不是第一个...足够我现在正在做的。感谢您的教育。 – StreaminJA 2011-05-25 04:30:14
这里的一个第二遍:
- (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];
}
def.text而不是def.txt – StreaminJA 2011-05-25 04:30:55
当然,two
和definition
字符串是过时的,因为它们是的nameSection
和five
相同副本。
如果您为我们描述了术语和信件集合以及您正在尝试做什么,这将会很有帮助。另外,看起来最后三行可以用“def.text = five”代替。 – 2011-05-24 18:32:49
如果你用简单的语言描述你想要完成的事情,这对我们(也许也是你自己)会有帮助。如果我不得不猜测它是这样的:从字符串中剥离换行符,括号和引号。检出NSMutableCharacterSet。 – kball 2011-05-24 18:34:13
它从中拉出的plist在每个数组中都有一组数组和一个NSString。我知道,我知道,但我只知道如何从一组数组中填充TableView。所有这些都是将每个数组的NSString显示到UITextView中。 – StreaminJA 2011-05-24 18:48:36