2012-02-19 38 views
0

我有一个简单而又相当前瞻的方法。 它应该创建一个文件夹,如果它不存在。 它需要一个正确声明的字符串参数。参数未在方法中收到

当我使用它并传递一个参数时,接收变量保持为空,这很奇怪,因为pathTo_Folder是一个路径。

任何想法,为什么会发生这种情况?

//Declaration in .h 
- (void) createFolder   : (NSString *) thePath ; 

//The call 
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    NSString *homePath = [@"~" stringByExpandingTildeInPath]; 
    NSString *pathTo_Folder = [NSString stringWithFormat:@"%@/Library/Application Support/prolog/",homePath]; 
    [self createFolder : pathTo_Folder]; 
} 


//In .m 
- (void) createFolder: thePath { 
    BOOL isDir; 
    NSFileManager *fileManager = [NSFileManager defaultManager]   ; 
    [fileManager fileExistsAtPath:thePath isDirectory: &isDir]   ; 

    NSLog(@"Folder '%@' exists: %d",thePath,isDir)      ; 

    if (isDir == FALSE) 
    { 
     [fileManager createDirectoryAtPath: thePath withIntermediateDirectories:YES attributes:nil error:nil]; 
    } 
} 
+2

你不觉得'thePath'应该有一个类型? – 2012-02-19 04:05:28

回答

1

我的猜测是因为你没有定义的thePath类型,编译器默认它的intint%@打印得非常好。

+0

感谢您的建议, 我将.h的定义从.h复制到.m像这样: - (void)createFolder:(NSString *)thePath; 现在它工作。 非常感谢。 Ronald --- – 2012-02-19 06:42:20

0

我没有看到参数thePath选择任何类型声明,它应该是

- (void) createFolder:(NSString*)thePath { 
    BOOL isDir; 

也许你没有得到一个警告,因为它有一个默认id但将主要解决的问题。但是,一个id类型将是确定在这种情况下,也许这是一些ObjC黑魔法..

0

这是一个有点清洁,应该工作:

- (void) createFolder: (NSString *) thePath; 

- (void) applicationDidFinishLaunching: (NSNotification *) aNotification 
{ 
    NSString *appSupportDir = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, 
    NSUserDomainMask, YES) lastObject]; 
    [self createFolder: [appSupportDir stringByAppendingPathComponent: @"prolog"]]; 
} 

- (void) createFolder: (NSString *) thePath 
{ 
    BOOL isDir; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if (![fileManager fileExistsAtPath: thePath isDirectory: &isDir]) { 
     [fileManager createDirectoryAtPath: thePath withIntermediateDirectories: YES attributes: nil error: nil]; 
    } 
}