2012-02-07 31 views

Trả lời

23

Thay đổi dòng đầu tiên để

NSMutableString *teststring = [NSMutableString string]; 
46

Bạn cần tạo chuỗi trước tiên.

NSMutableString *teststring = [[NSMutableString alloc]init]; 

[teststring appendString:@"hey"]; 

NSLog(teststring); 

Bây giờ, nó sẽ in.

+0

Cảm ơn, cũng phù hợp này cho tôi. –

9

Dòng này

NSMutableString *teststring; 

chỉ đơn giản là thiết lập một con trỏ, nhưng không tạo ra bất cứ điều gì. Thay vào đó, bạn cần phải tạo một đối tượng mới của NSMutableString, ví dụ:

NSMutableString *teststring = [[NSMutableString alloc] init]; 
[teststring appendString:@"hey"]; 
NSLog("%@", teststring); 
2

mẫu:

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 
0
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); 
    } 
Các vấn đề liên quan