如何附加一个string到NSMutableString
我是一个绝对的新手与目标C
用这个代码
NSMutableString *teststring; [teststring appendString:@"hey"]; NSLog(teststring);
没有任何东西显示在控制台中。
当然,我在这里做错了什么… 🙂
将第一行更改为
NSMutableString *teststring = [NSMutableString string];
您需要先创buildstring。
NSMutableString *teststring = [[NSMutableString alloc]init]; [teststring appendString:@"hey"]; NSLog(teststring);
现在,它会打印。
这条线
NSMutableString *teststring;
只是build立一个指针,但不创build任何东西。 相反,您需要创build一个新的NSMutableString
实例,例如:
NSMutableString *teststring = [[NSMutableString alloc] init]; [teststring appendString:@"hey"]; NSLog("%@", teststring);
样品:
NSMutableString *buffer = [[NSMutableString alloc] init]; // retain count = 1. Because of the "alloc", you have to call a release later [buffer appendString:@"abc"]; NSLog(@"1 : %@", buffer); [buffer appendString:@"def"]; NSLog(@"2 : %@", buffer); [buffer release]; // retain count = 0 => delete object from memory
NSMutableString *tag = [NSMutableString stringWithString: @"hello <td> this is inside td </td> 000999 <><> ..<. ><> 00000 <td>uuuuu</td> vvvvv <td> this is also inside td </td>"]; NSRange open = [tag rangeOfString:@"<"]; while(open.location != NSNotFound) { NSRange close = [tag rangeOfString:@">"]; NSRange string = NSMakeRange(open.location, close.location-open.location+1); [tag replaceCharactersInRange:string withString:@""]; open = [tag rangeOfString:@"<"]; NSLog(@"%@",tag); }