存储数据时出现问题

发布于 2024-12-04 11:21:12 字数 1245 浏览 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);
    }
    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); 打印一个空字符串! (数据存储在设备中,因为当字符串!=空时我至少运行过一次我的应用程序 谢谢

I can't getting my stored data.... This is the code:

 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];}

Is there something wrong?

EDIT
No bugs no crashes but the NSLog(@"string recuperata %@",string); prints an empty string! (data are stored in the device because I had running at least one time my app when string!= empty
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你げ笑在眉眼 2024-12-11 11:21:13
    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];

    }
    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];

    }
给不了的爱 2024-12-11 11:21:12

我认为第二个 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];

I think there is a else missing on the second if. I'm not sure if that is what you wanted or if it is a typo on your part. check if that is the problem.

 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];
一江春梦 2024-12-11 11:21:12

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

Try changing to NSUTF8StringEncoding if you are unsure about the encoding, usually it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文