1

Suppose I have a custom function called savePlist as below.

CommonClass.m

- (NSString *)getDirectoryPath
{
    NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [pathList objectAtIndex:0];
    return path;
}

- (void)savePlist:(NSString *)fileName WithArray:(NSArray *)fileArr
{
    NSString *path = [[self getDirectoryPath] stringByAppendingPathComponent:fileName];
    [fileArr writeToFile:path atomically:YES];
}

While I run a code as below, no plist is generated.

ABCAppDelegate.m

...
[cc savePlist:@"example.plist" WithArray:[NSArray new]];

The file of example.plist has not been generated, is there any mistakes on my code?

Thanks

UPDATE:

If I use the code as below, the xxx.plist file has been generated successfully.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *plistFile = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"abc.plist"];
NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFile];

if (!plist) {
    plist = [NSMutableDictionary new];
    [plist writeToFile:plistFile atomically:YES];
    NSLog(@"write plist");
}

Reference: link

UPDATE 2:

I change the code as below:

NSLog(@"code");
[cc savePlist:@"example.plist" WithArray:[NSArray new]];
NSLog(@"code");

- (void)savePlist:(NSString *)fileName WithArray:(NSArray *)fileArr
{
    NSLog(@"fileName = %@, fileArr = %@", fileName, fileArr);
    NSString *path = [[self getDirectoryPath] stringByAppendingPathComponent:fileName];
    NSLog(@"path = %@", path);
    [fileArr writeToFile:path atomically:YES];
}

It just print out:

2011-10-10 14:24:00.560 ABC[3390:207] code
2011-10-10 14:24:00.561 ABC[3390:207] code

No message of fileName = and fileArr =, also path = print out on the log, so the code inside savePlist have not been executed?

Community
  • 1
  • 1
Charles Yeung
  • 38,347
  • 30
  • 90
  • 130
  • 1
    Here are a couple of things to verify. Did you try passing something other than an empty array? Did you print out the result of getDirectoryPath and path to verify they are what you expect? Did you check that writeToFile returns YES? – D.C. Oct 10 '11 at 05:15
  • I agree with darren, this is most likely a path. I suggest adding NSLog(@"%@",path); just before writeToFile message to debug the file path. – EladG Oct 10 '11 at 06:13
  • Ok, thanks for advise, I have added UPDATED in my question, please kindly check. – Charles Yeung Oct 10 '11 at 06:28

1 Answers1

0

It looks like your common class object is not instantiated, and you are sending all those messages to nil. Try logging cc in your app delegate code, where you call this method from.

You need to create an instance of your common class, something like

cc = [[CommonClass alloc] init];

(may vary depending on your set up).

jrturton
  • 118,105
  • 32
  • 252
  • 268