使用IOS保存除Plist之外的其他类型

时间:2012-09-18 05:01:09

标签: iphone objective-c ios nsmutablearray plist

这就是我实际保存可变数组的方法。

[myMutableArray writeToFile:@"/Users/myUserIdentifier/Desktop/results.plist" atomically:YES]; 

该数组基本上包含很多单词,我希望Windows用户能够阅读它们,我需要它们返回每个单词的新行。

我尝试在每个单词的末尾添加标签,然后将文件保存在.html中:

[myMutableArray writeToFile:@"/Users/myUserIdentifier/Desktop/results.html" atomically:YES]; 

实际上文件是可读的,但是标签的格式太差了:

  <br> 

因此Chrome不会将其理解为“转到新线”。

好吧,相当混乱的解释,希望我能找到一个心灵混乱的灵魂伴侣来理解这些东西!

3 个答案:

答案 0 :(得分:0)

您尝试将NSMutableArray保存为数组然后读取但是 既不起作用:

 - (void)saveState
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@\"Layers\" ofType:@\"plist\"];  
NSMutableDictionary* layersDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

[layersDict setValue:layers forKey:@\"Layers\"];
[layersDict writeToFile:filePath atomically: YES];
}

- (void)loadLastState;
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@\"Layers\" ofType:@\"plist\"];
NSMutableDictionary* layersDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

NSArray *layersArray = [layersDict objectForKey:@\"Layers\"];

NSMutableArray *layersMutable = [[NSMutableArray alloc] initWithArray:layersArray];

[self recoverFromLayers:layersMutable];
}

答案 1 :(得分:0)

是的,不是真的。一切正常。我只是想在启动保存的文件时格式化我的结果以使Windows用户更清楚一点。到目前为止,HTML是最好的。但我坚持使用那些标签。也许还有另一种简单的方法来获取一个文本文件,每个单词都会转到下一行。

答案 2 :(得分:0)

您只想将包含字符串的数组写入文件?那不需要什么花哨的东西。例如:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
    NSMutableArray *words = [NSMutableArray arrayWithObjects:@"fluffy",@"furball",@"scratcher",nil];
    FILE *filePointer = fopen("/Volumes/Weinberg/Users/alan/Desktop/cats.txt","w");
    [words enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        fprintf(filePointer,"%s\n",[obj UTF8String]);
    }];
    fclose(filePointer);
    [p release];
}