stringByAppendingPathComponent, hows it work?

前端 未结 6 1294
暗喜
暗喜 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 23:07

    You don't append the delimiter. You append the next path component (eg filename, dir, etc). This avoids you needing to know the delimiter for your particular system.

    NSMutableString* mutablePath = [NSMutableString string];
    NSString* fullPath = [rootPath stringByAppendingPathComponent:filename];
    
    [mutablePath setString:fullPath]; // OK to setString: of Mutable with non-Mutable
    [mutablePath appendString:someOtherString]; // This won't cause an exception
    
    // Example to clarify on comments below
    {
        // This will cause a compiler warning.
        // warning: incompatible Objective-C types assigning
        //    ‘struct NSString *’, expected ‘struct NSMutableString *’
        NSMutableString* ms = [@"FOO" stringByAppendingPathComponent:@"BAR"];
    }
    

    There is a fairly clear example in the documentation.

提交回复
热议问题