[英]How to append a string to NSMutableString
我是Objective-c的绝对新手
用这个代码
NSMutableString *teststring;
[teststring appendString:@"hey"];
NSLog(teststring);
控制台中没有显示任何内容。
当然我在这里做错了...... :-)
您需要先创建字符串。
NSMutableString *teststring = [[NSMutableString alloc]init];
[teststring appendString:@"hey"];
NSLog(teststring);
现在,它将打印出来。
将第一行更改为
NSMutableString *teststring = [NSMutableString string];
这条线
NSMutableString *teststring;
只是建立一个指针,但不创建任何东西。 相反,您需要创建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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.