开发者

Problem storing data

开发者 https://www.devze.com 2023-04-05 01:23 出处:网络
I can\'t getting my stored data.... This is the code: if ([string isEqualToString:@\"\"]) { //RECUPERO DATA

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 = NSSearchPathForDirectoriesInDo开发者_如何学Cmains(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


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


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


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

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消