stringByAppendingPathComponent, hows it work?

前端 未结 6 1295
暗喜
暗喜 2021-01-04 22:37

EDIT_v002

I have had a look at all the comments and I am starting to see what I should be doing. To that end I have modified my code (see below) I have changed new

6条回答
  •  醉话见心
    2021-01-04 22:53

    All the existing answers are leaking the original testPath string. For something as simple as this, why has nobody recommended -[NSMutableString appendString:] intead?

    [testPath appendString:@"/"];
    

    There's no equivalent to -stringByAppendingPathComponent: for NSMutableString, but it looks like he's just trying to add a slash, not a path component anyway. If you really wanted to add a path component, you could do this:

    [testPath setString:[testPath stringByAppendingPathComponent:@"..."]];
    

    It's an annoying workaround, but as @dreamlax points out, -stringByAppendingPathComponent: always returns an immutable string, even when called on an NSMutableString object. :-(

提交回复
热议问题