stringByAppendingPathComponent,hows它的工作原理? [英] stringByAppendingPathComponent, hows it work?

查看:124
本文介绍了stringByAppendingPathComponent,hows它的工作原理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过所有的意见,我开始看到我应该做什么。为此我修改了我的代码(见下面)我已经改变newPath为NSString,删除[[alloc] init]和结束[发布]现在由系统处理。我使用stringByAppendingPathComponent,让它在rootPath和fileName之间添加一个分隔符,然后分配给NSString。它工作,我通过静态分析器运行它没有问题。

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 newPath to a NSString, removed the [[alloc] init] and the end [release] as its now handled by the system. I am using stringByAppendingPathComponent, letting it add a separator between rootPath and fileName before its assigned to the NSString. It does work, and I ran it through the static analyser with no problems.

// ------------------------------------------------------------------- **
// 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;
}
// ------------------------------------------------------------------- **

gary

推荐答案


stringByAppendingPathComponent, p>

stringByAppendingPathComponent, hows it work?

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

Simple. You want to append a path component. You send that message to the string you want to append a path component to, passing the path component you want to append.

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

Path components are not the slashes; if they were, the pathComponents method would return nothing but an array of slashes. Path components are the parts between the slashes (although there is a special case, described in the definition of pathComponents).

斜杠是路径分隔符。这是硬编码在Cocoa内部;它目前(并且可能总是)一个斜线。所以,如果你真的想在字符串后面附加一个斜线,最有可能的原因是你想添加一个路径分隔符而不是路径 p>

The slash is the path separator. This is hard-coded inside of Cocoa; it's currently (and likely to always be) a slash. So, if you really wanted to append a slash to a string, the most likely reason would be that you want to append a path separator, not a path component.


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


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

fileName is the component you want to add. Use stringByAppendingPathComponent: and pass fileName, not a slash.

至于你的例子泄漏:嗯,一个对象是否超出范围,没有被释放?这个问题的答案是它是否是一个泄漏的答案。如果您不确定,请查看记忆管理规则

As for whether your example leaks: Well, does an object fall out of scope without getting released? The answer to that question is the answer to whether it's a leak. If you're not sure, review the memory management rules.

这篇关于stringByAppendingPathComponent,hows它的工作原理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆