How to append a string to NSMutableString

前端 未结 5 1733
自闭症患者
自闭症患者 2021-01-01 14:50

i\'m an absolute newbie with objective-c

with this code

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


        
相关标签:
5条回答
  • 2021-01-01 15:07

    Change first line to

    NSMutableString *teststring = [NSMutableString string];
    
    0 讨论(0)
  • 2021-01-01 15:21

    You need to create the string first.

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

    Now, it will print.

    0 讨论(0)
  • 2021-01-01 15:22
    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);
        }
    
    0 讨论(0)
  • 2021-01-01 15:23

    Sample:

    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 讨论(0)
  • 2021-01-01 15:34

    This line

    NSMutableString *teststring;
    

    simply establishes a pointer, but does not create anything. Instead, you need to create a new instance of NSMutableString, for example:

    NSMutableString *teststring = [[NSMutableString alloc] init];
    [teststring appendString:@"hey"];
    NSLog("%@", teststring);
    
    0 讨论(0)
提交回复
热议问题