存储数据时出错

时间:2011-09-13 12:25:22

标签: iphone objective-c storing-data

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

 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);打印一个空字符串! (数据存储在设备中,因为当字符串!=空时,我的应用程序至少运行了一次 感谢

3 个答案:

答案 0 :(得分: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);
    } 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];

答案 1 :(得分:0)

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

答案 2 :(得分: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];

    }