Why is NSString stringWithString returning pointer to copied string?

后端 未结 2 1910
你的背包
你的背包 2021-01-19 09:42

I\'m trying to copy an NSString value out of an NSMutableArray into a new variable. NSString stringWithString is returning an NS

2条回答
  •  离开以前
    2021-01-19 10:24

    Since NSString instances are not mutable, the +stringWithString: method is simply returning the input string with an incremented reference count.

    If you really want to force the creating of a new, identical string, try:

    NSString * copy = [NSString stringWithFormat:@"%@", [arr objectAtIndex:0]];
    

    There is little point in doing so, though, unless you need the pointer to be unique for some other reason...

提交回复
热议问题