2013-03-24 78 views
2

我想要在xcode的plist文件中放入数据字符串(例如用于创建多个url的循环)。 这是我的代码(循环)如何在.plist文件中添加数据字符串?

int count = 5; 
NSString *a; 
NSMutableArray *b = [[NSMutableArray alloc] initWithCapacity:count]; 

for (int i=1; i<= count; i++) { 

     a = [NSString stringWithFormat:@"http://192.168.1.114:81/book.php?page=%d",i]; 
     [b addObject:a]; 

    } 

现在我想挽救顶级代码的网页中的.plist文件的一个行,但我不知道我能做些什么?

你可以指导我吗?

回答

0

尝试[b writeToFile:@"myFile.plist" atomically:YES];,但要确保数组中的所有数据都可以用plist表示。

3

我不知道你拍摄相当的东西,但如果你试图从这些URL字符串的HTML,你也许可以这样做:

// build path for filename 

NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 
NSString *filename = [docsPath stringByAppendingPathComponent:@"test.plist"]; 

// create array of html results 

NSMutableArray *htmlResults = [NSMutableArray array]; 
for (NSString *urlString in b) 
{ 
    // get the html for this URL 

    NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil]; 

    // add the html to our array (or zero length string if it failed) 

    if (html) 
     [htmlResults addObject:html]; 
    else 
     [htmlResults addObject:@""]; 
} 

// save the html results to plist 

[htmlResults writeToFile:filename atomically:YES]; 

一对夫妇的想法:

  1. 取决于有多少页,我不确定是否疯狂将所有页面加载到plist。我想无论是

    • 使用像核心数据的一些持久存储,所以我没有保存所有内存中的页面,或

    • 做HTML(加载它的一些延迟加载我需要它))

  2. 另外,如果我要加载的所有网页,因为它可能需要一点时间,我可能有,我有我的进步更新进度来看,这样的用户在下载过程中不会查看冻结屏幕。

  3. 如果你只是想检索一个单独的html文件,那么将其存储在plist中可能没有意义。我只需将html写入一个文件(一个HTML文件,而不是plist)。

  4. 我一般不喜欢加载主队列中的html。我会做一个dispatch_async在后台队列中执行此操作。但是我很犹豫,直到你澄清你正在寻找的东西。

但希望这可以指导您正确的方向,向您展示如何从网页中检索数据。


如果你想个别的HTML文件保存到一些地方的文件(比如说X.html其中X是从零开始的索引号),你可以这样做:

// identify the documents folder 

NSString *docsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 

// save the html results to local files 

[b enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 
    NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:obj] encoding:NSUTF8StringEncoding error:nil]; 
    if (html) 
    { 
     NSString *filename = [docsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.html", idx]]; 
     [html writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:nil]; 
    } 
}]; 
相关问题