Jack
Jack

Reputation: 3878

iPhone wait until code is executed

I am creating a method that is run once a user presses a button. The method creates a folder given the name defined by the user and adds a plist to that folder. The problem is that it doesn't create the plist. I know that the code works seperately, but I think that the plist creation is being done before the directory has been created. Is there any way to add a 'wait until' clause? Thanks

- (void) addTheDirectory{
NSString *theTitle = theTitleField.text;
NSString *theDescription = theDescriptionField.text;

//create the directory
[[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithFormat:@"%@/%@", DOCUMENTS_FOLDER, theTitle] attributes:nil];

//create the plist
NSMutableDictionary* plistDictionary = [[NSMutableDictionary alloc] init];
[plistDictionary setValue:theTitle forKey:@"Title"];
[plistDictionary setValue:theDescription forKey:@"Description"];
[plistDictionary setValue:@"0.1" forKey:@"LBVersion"];
[plistDictionary writeToFile:[NSString stringWithFormat:@"%@/%@/info.plist", DOCUMENTS_FOLDER, theTitle] atomically: YES];
}

Upvotes: 0

Views: 626

Answers (1)

TechZen
TechZen

Reputation: 64428

createDirectoryAtPath:attributes: returns a BOOL so the code will not progress until the method creates the directory and returns.

I think this construction...

[NSString stringWithFormat:@"%@/%@/info.plist", DOCUMENTS_FOLDER, theTitle]

... is most likely your problem. It will usually work but the more robust method would be to use one of the dedicated path methods such as:

[NSString pathWithComponents:[NSArray arrayWithObjects:DOCUMENTS_FOLDER, theTitle,@"/info.plist",nil]]

This makes it more likely that you will get a correct path, especially if you just altered it.

You should also capture the boolean return from both file operations so you can see which one fails.

Upvotes: 2

Related Questions