9 Replies Latest reply: Jan 24, 2013 5:53 PM by red_menace
letatanu Level 1 Level 1 (0 points)

Hi everyone, I have NSData to create a file *.txt and then I read it by using NSBundle, but it do not working correctly. Can anybody help me?

  • 1. Re: How to use NSData and NSBundle together?
    red_menace Level 6 Level 6 (14,615 points)

    What does "not working correctly" mean?  No one is going to be able to help much without a detailed description of the problem (including the versions of OS and Xcode being used), and relevant details such as code snippets and error logs.

  • 2. Re: How to use NSData and NSBundle together?
    letatanu Level 1 Level 1 (0 points)

    Thanks for your care.  I use Xcode 4.5.2, and build on iOS 6.0. Here my code :

    -(void)WriteToFile

    {

        NSString * temp=[[NSString alloc]init];

        for(NSString *word in listMenu)

        {

            NSString* word1=[NSString stringWithFormat:@"%@#Waiting",word];

            temp=[NSString stringWithFormat:@"%@?%@",temp,word1];

        }

        [self creatFile:[[UIDevice currentDevice]name] data:temp];

    }

     

    -(void)creatFile:(NSString *)fileName data:(NSString*)data

    {

        NSString *pathFile=[NSString stringWithFormat:@"%@_%d.txt",fileName,Ordinal];

        NSData * dataPath=[data dataUsingEncoding:NSUTF8StringEncoding];

        [dataPath writeToFile:pathFile atomically:YES];

    }


    I use function "WritetoFile" to create file and I use this code to read file:

    -(void)ReadFile

    {

        OrdinalNumber=1;

        NSString * nameFile=[NSString stringWithFormat:@"%@_%d",[[UIDevice currentDevice]name],OrdinalNumber];

        NSString *pathname=[[NSBundle mainBundle]pathForResource:nameFile ofType:@"txt" inDirectory:@"/"];

        NSString *wordstring =[NSString stringWithContentsOfFile:pathname];

        NSArray *temp=[wordstring componentsSeparatedByString:@"?"];

        listMenu=[[NSMutableArray alloc]initWithArray:temp];

        [listMenu removeObjectAtIndex:0];           

          

    }

    However, I just recieve the previous data, not current. What's wrong with it?

  • 3. Re: How to use NSData and NSBundle together?
    red_menace Level 6 Level 6 (14,615 points)

    You aren't really trying to write to the application bundle, are you? 

    I'm not familiar with IOS, but take another look at your file create/write methods.

  • 4. Re: How to use NSData and NSBundle together?
    xnav Level 5 Level 5 (6,630 points)

    writeToFile: returns a BOOL indicating if it was sucessful.  You may want to check.

  • 5. Re: How to use NSData and NSBundle together?
    letatanu Level 1 Level 1 (0 points)

    I dont know WriteToFile by NSBundle. Can you show me?

  • 6. Re: How to use NSData and NSBundle together?
    letatanu Level 1 Level 1 (0 points)

    As I said, WriteToFile is successful, but ReadFile just read the previous data, NOT CURRENT. how to solve it?

  • 7. Re: How to use NSData and NSBundle together?
    red_menace Level 6 Level 6 (14,615 points)

    WriteToFile is successful

     

    You might want to check that - I can't tell what winds up as the write path, but it doesn't look like you are writing to the same file path that you are reading from.  If you are trying to write to the application bundle (where you are reading from) that is not a good idea.  Apart from reading and writing a file, exactly what is it you are wanting to do?

  • 8. Re: How to use NSData and NSBundle together?
    letatanu Level 1 Level 1 (0 points)

    Ok, maybe you are right. And now I want to use NSBundle to read this file. Could you show me?

  • 9. Re: How to use NSData and NSBundle together?
    red_menace Level 6 Level 6 (14,615 points)

    The NSBundle class has some methods for finding resources in the application bundle, if that is where your file is located.  You just need to provide a valid path for your file read/write, wherever that is.  If you are trying to use some kind of preference, look at NSUserDefaults, if you are using one of the standard directories such as the temporary directory, look at NSFileManager, etc.