+[NSString stringWithString:] — what's the point?

后端 未结 7 1780
悲&欢浪女
悲&欢浪女 2020-12-01 16:20

As NSString strings are immutable, what is the value of the stringWithString: class method?

I get the utility when used with NSMutabl

7条回答
  •  旧时难觅i
    2020-12-01 16:38

    You might have a NSMutableString (or some home-grown NSString subclass) that you want to duplicate.

    NSMutableString *buffer = [NSMutableString string];
    // do something with buffer
    NSString *immutableStringToKeepAround = [NSString stringWithString:buffer];
    

    Of course, you can also just make a copy:

    NSMutableString *buffer = [NSMutableString string];
    // do something with buffer
    NSString *immutableStringToKeepAround = [[buffer copy] autorelease];
    

    but you own the copy and must release or autorelease it.

提交回复
热议问题