ฉันมีปัญหาในการสร้างไดเรกทอรีและไฟล์ด้วย NSFileManager บนอุปกรณ์ iPhone รหัสของฉันที่แสดงด้านล่างทำงานได้ดีบนเครื่องจำลอง แต่ไม่ใช่บนอุปกรณ์ คุณช่วยฉันหน่อยได้ไหม ขอคำแนะนำว่าปัญหาอาจเกิดจากอะไร ขอบคุณสำหรับทุกคำตอบ..
ฉันกำลังสร้างไดเรกทอรีเป็นครั้งแรกด้วยวิธีนี้:
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSArray *arPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
NSString *appPath = [[NSString alloc] init];
appPath = [arPaths objectAtIndex:0];
strDownloadsDir = [[NSString alloc] init];
strDownloadsDir = [[appPath stringByAppendingPathComponent:@"/Other"] copy];
if(![fileMgr fileExistsAtPath:strDownloadsDir])
[fileMgr createDirectoryAtPath:strDownloadsDir attributes:nil];
จากนั้นฉันพยายามสร้างไฟล์ใหม่ในไดเร็กทอรีนี้ด้วยวิธีนี้:
NSString *filePath = [strDownloadsDir stringByAppendingPathComponent:strDlFileName];
//Test whether this file exists, if not, create it
NSLog(@"%@", filePath);
if(![fileMgr fileExistsAtPath:filePath])
{
if([fileMgr createFileAtPath:filePath contents:nil attributes:nil])
NSLog(@"Creating new file at path %@", filePath);
else
NSLog(@"Failed to create new file.");
}
ดูเหมือนว่ามีบางอย่างผิดปกติกับ NSFileManager ทั้งหมด เพราะเมื่อฉันใช้ fileExistAtPath กับเส้นทางที่กำหนดโดยสิ่งนี้
NSArray *arPaths = NSSearchPathForDirectoriesInDomains(NSDownloadsDirectory, NSUserDomainMask, YES);
NSString *appPath = [[NSString alloc] init];
appPath = [arPaths objectAtIndex:0];
มันใช้งานไม่ได้เช่นกัน ฉันพยายามเปลี่ยนไดเร็กทอรีเป็น NSDocumentsDirectory แต่ก็ไม่ได้ช่วยอะไร
[[NSString alloc] init];
- person kennytm   schedule 11.01.2010