stringByAppendingPathComponent,它有效吗?

时间:2009-09-28 21:40:49

标签: objective-c cocoa

EDIT_v002

我已经看了所有的评论,我开始看到我应该做什么。为此我修改了我的代码(见下文)我已将newPath更改为NSString,删除了[[alloc] init]和end [release],因为它现在由系统处理。我正在使用stringByAppendingPathComponent,让它在rootPath和fileName分配给NSString之前添加一个分隔符。它确实有效,我通过静态分析仪运行它没有任何问题。

// ------------------------------------------------------------------- **
// DISC: FILEWALKER ..... (cocoa_fileWalker.m)
// DESC: List all "*.png" files in specified directory
// ------------------------------------------------------------------- **
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *fileName;
    NSDictionary *attrDir;
    NSError *myError;
    NSNumber *fileSize;
    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *rootPath = [@"~/Pictures/Ren/PRMan" stringByExpandingTildeInPath];
    NSString *newPath;

    NSLog(@"Home: %@",rootPath);

    for(fileName in [manager enumeratorAtPath:rootPath]){
        if ([[fileName pathExtension] isEqual:@"png"]) {    

            newPath = [rootPath stringByAppendingPathComponent:fileName];   
            attrDir = [manager attributesOfItemAtPath:newPath error:&myError];
            fileSize = [attrDir objectForKey: @"NSFileSize"];
            NSLog(@"File: %@ Size: %@", newPath, fileSize);
        }
    }
    [pool drain];
    return 0;
}
// ------------------------------------------------------------------- **

加里

6 个答案:

答案 0 :(得分:15)

  

stringByAppendingPathComponent,它有效吗?

简单。您想要追加路径组件。您将该消息发送到要附加路径组件的字符串,并传递要追加的路径组件。

路径组件不是斜杠;如果它们是,pathComponents方法将只返回一个斜杠数组。路径组件是斜杠之间的部分(虽然有一个特殊情况,在pathComponents的定义中描述)。

斜杠是路径分隔符。这在Cocoa中是硬编码的;它目前(并且可能永远是)斜线。因此,如果您真的想在字符串中附加斜杠,最可能的原因是您要附加路径 separator ,而不是路径组件

    [newPath setString:rootPath];
    [newPath appendString:@"/"];
    [newPath appendString:fileName];

fileName是您要添加的组件。使用stringByAppendingPathComponent:并传递fileName,而不是斜杠。

至于你的例子是否泄漏:那么,一个对象是否会在没有被释放的情况下超出范围?这个问题的答案就是它是否是泄漏的答案。如果您不确定,请查看the memory management rules

答案 1 :(得分:5)

您不附加分隔符。您追加下一个路径组件(例如filename,dir等)。这可以避免您需要知道特定系统的分隔符。

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"];
}

the documentation.

中有一个相当明显的例子

答案 2 :(得分:5)

所有现有答案都泄漏了原始的testPath字符串。对于像这样简单的事情,为什么没有人推荐-[NSMutableString appendString:] intead?

[testPath appendString:@"/"];

对于NSMutableString,没有等效的-stringByAppendingPathComponent:,但看起来他只是想尝试添加斜杠,而不是路径组件。如果你真的想添加一个路径组件,你可以这样做:

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

这是一个令人烦恼的解决方法,但正如@dreamlax指出的那样,-stringByAppendingPathComponent:总是返回一个不可变的字符串,即使在NSMutableString对象上调用也是如此。 : - (

答案 3 :(得分:2)

-stringByAppendingPathComponent返回一个新的不可变字符串,它不会修改原始字符串。您必须使用此方法的返回值。

答案 4 :(得分:1)

最后一行应该是:

testPath = [testPath stringByAppendingPathComponent:@"/"];

答案 5 :(得分:0)

[testPath stringByAppendingString:@"/"]