[英]NSFileManager Cant create file
我有這個代碼。 此代碼不會在文檔目錄中創建文件,我不知道為什么。 任何人都可以看到我錯過了什么。 “數據”字典中有我需要的內容,但是當涉及到 writeToFile 時:它沒有發生,因為沒有創建文件。 相同的代碼在我擁有的其他應用程序中也有效,我肯定遺漏了一些東西。
- (void)addBookmark{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: [self filePathOfBookmarks]])
{
[self filePathOfBookmarks];
}
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
if ([fileManager fileExistsAtPath: [self filePathOfBookmarks]])
{
data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
}
else
{
// If the file doesn’t exist, create an empty dictionary
data = [[NSMutableDictionary alloc] init];
}
NSMutableDictionary *firstOne = [NSMutableDictionary dictionary];
[firstOne setObject:@"one" forKey:@"name"];
[firstOne setObject:@"two" forKey:@"call"];
[firstOne setObject:@"twoandhalf" forKey:@"email"];
[firstOne setObject:@"four" forKey:@"description"];
[data setObject:firstOne forKey:@"ID"];
[data writeToFile: [self filePathOfBookmarks] atomically:YES];
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
NSLog(@"file content %@",savedStock);
}
- (NSString *) filePathOfBookmarks {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
return [docDirectory stringByAppendingPathComponent:@"favourites"];
}
NSLog(@"%@",[self filePathOfBookmarks]);
這返回:
/Users/spire/Library/Application Support/iPhone Simulator/5.1/Applications/40CAA37F-6477-4101-A142-D4797748ABD9/Documents/favorites
請參閱下面的代碼,它在我這邊工作得很好。 您剛剛在 filePathOfBookmarks 方法中提到了目錄而不是確切的文件名。 我可以將數據保存在 favourites.txt 文件中。
您還可以參考以下鏈接以獲取有關 iOS 中文件處理的更多信息
2) iPhone文件系統
- (void)addBookmark{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: [self filePathOfBookmarks]])
{
[self filePathOfBookmarks];
}
NSMutableDictionary *data;// = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
if ([fileManager fileExistsAtPath: [self filePathOfBookmarks]])
{
data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
}
else
{
// If the file doesn’t exist, create an empty dictionary
data = [[NSMutableDictionary alloc] init];
}
NSMutableDictionary *firstOne = [NSMutableDictionary dictionary];
[firstOne setObject:@"one" forKey:@"name"];
[firstOne setObject:@"two" forKey:@"call"];
[firstOne setObject:@"twoandhalf" forKey:@"email"];
[firstOne setObject:@"four" forKey:@"description"];
[data setObject:firstOne forKey:@"ID"];
[data writeToFile: [self filePathOfBookmarks] atomically:YES];
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
NSLog(@"file content %@",savedStock);
}
- (NSString *) filePathOfBookmarks {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
return [docDirectory stringByAppendingPathComponent:@"favourites.txt"];
}
你的一些代碼沒有意義(對我來說)。
這沒有做任何事情:
if (![fileManager fileExistsAtPath: [self filePathOfBookmarks]])
{
[self filePathOfBookmarks];
}
然后你用你的書簽文件內容初始化你的數據 object - 此時可能文件不存在:
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
您可以簡單地省略上面的兩個代碼塊,因為您之后會再次檢查它:
if ([fileManager fileExistsAtPath: [self filePathOfBookmarks]])
{
data = [[NSMutableDictionary alloc] initWithContentsOfFile: [self filePathOfBookmarks]];
}
else
{
// If the file doesn’t exist, create an empty dictionary
data = [[NSMutableDictionary alloc] init];
}
這是有道理的。 但是當您省略前兩個代碼塊時,請確保聲明數據 object:
NSMutableData *data;
然后將第一個 object 設置為數據字典。 這看起來不錯(盡管使用此代碼,但根本不使用之前從現有書簽文件加載的數據)。
編寫數據字典似乎也不錯:
[data writeToFile: [self filePathOfBookmarks] atomically:YES];
當然,您的方法 filePathOfBookmarks 會返回一個有效的文件名。
然而,該方法返回一個目錄,而不是一個文件。 像這樣添加后綴:
return [docDirectory stringByAppendingPathComponent:@"favorites.plist"];
使用檢查文件路徑的有效性
NSLog(@"%@",[self filePathOfBookmarks]);
在寫入文件之前。
也試試
NSLog(@"%@",data);
在編寫字典之前查看字典包含的內容。
如果仍然無效,請重新發布。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.