2012-02-07 12 views
16

Jestem absolutnym nowicjuszem z Objective-CJak dołączyć ciąg NSMutableString

z tym kodem

NSMutableString *teststring; 
[teststring appendString:@"hey"]; 
NSLog(teststring); 

nic nie zostanie wyświetlony w konsoli.

pewnością robię coś źle tutaj ... :-)

Odpowiedz

23

Zmień pierwszą linię do

NSMutableString *teststring = [NSMutableString string]; 
46

Musisz utworzyć ciąg pierwszy.

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

[teststring appendString:@"hey"]; 

NSLog(teststring); 

Teraz zostanie wydrukowane.

+0

Dzięki, to dobrze pasują do mnie. –

9

Linia ta

NSMutableString *teststring; 

prostu ustanawia wskaźnik, ale nie tworzy niczego. Zamiast tego, należy utworzyć nową instancję NSMutableString, na przykład:

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

Próbka:

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); 
    }