0

我无法获取我存储的数据......这是代码:

 if ([string isEqualToString:@""]) {
        //RECUPERO DATA 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *pathToDocuments=[paths objectAtIndex:0];
        pathToDocuments=[pathToDocuments stringByAppendingString:@"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
        NSLog(pathToDocuments);
        dataReply=[[NSData alloc] initWithContentsOfFile:pathToDocuments];
        NSString *string = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];
        NSLog(@"string recuperata %@",string);
    }
    if ([string isEqualToString:@""]==NO) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *pathToDocuments=[paths objectAtIndex:0];
        pathToDocuments=[pathToDocuments stringByAppendingString:@"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
        NSLog(pathToDocuments);
        [dataReply writeToFile:pathToDocuments atomically:YES];}

有什么不对?

编辑 没有错误没有崩溃,但NSLog(@"string recuperata %@",string);打印一个空字符串!(数据存储在设备中,因为当字符串时我至少运行了一次我的应用程序!= 空谢谢

4

3 回答 3

0
    if ([string length]<=0) {
            //RECUPERO DATA 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *pathToDocuments=[paths objectAtIndex:0];
        pathToDocuments=[pathToDocuments stringByAppendingString:@"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
        NSLog(pathToDocuments);
        dataReply=[[NSData alloc] initWithContentsOfFile:pathToDocuments];
        NSString *string = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];
        NSLog(@"string recuperata %@",string);
    }

    else {

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *pathToDocuments=[paths objectAtIndex:0];
        pathToDocuments=[pathToDocuments stringByAppendingString:@"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
        NSLog(pathToDocuments);
        [dataReply writeToFile:pathToDocuments atomically:YES];

    }
于 2011-09-13T12:38:35.670 回答
0

我认为第二个 if 缺少 else 。我不确定这是您想要的还是您的错字。检查这是否是问题所在。

 if ([string isEqualToString:@""]) {
        //RECUPERO DATA 
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *pathToDocuments=[paths objectAtIndex:0];
        pathToDocuments=[pathToDocuments stringByAppendingString:@"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
        NSLog(pathToDocuments);
        dataReply=[[NSData alloc] initWithContentsOfFile:pathToDocuments];
        NSString *string = [[NSString alloc] initWithData:dataReply encoding:NSASCIIStringEncoding];
        NSLog(@"string recuperata %@",string);
    } else if ([string isEqualToString:@""]==NO) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *pathToDocuments=[paths objectAtIndex:0];
        pathToDocuments=[pathToDocuments stringByAppendingString:@"getSubscriptionsListShowOnlyWithUnreadFeeds.txt"];
        NSLog(pathToDocuments);
        [dataReply writeToFile:pathToDocuments atomically:YES];
于 2011-09-13T12:30:57.827 回答
0

NSUTF8StringEncoding如果您不确定编码,请尝试更改为,通常它可以工作。

于 2011-09-13T12:32:28.653 回答