Results 1 to 3 of 3
Discuss [Advice] Reading & Writing Data in Objective-C ? at the iPhone Developer Exchange - Hackint0sh.org; I am trying to write an NSString to a file and then read the NSString ...
  1. #1
    Rookie Array

    Join Date
    Feb 2008
    Posts
    12
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Unhappy [Advice] Reading & Writing Data in Objective-C ?

    I am trying to write an NSString to a file and then read the NSString and print it on a label in my iphone program. When I call my applicationDataFromFile method, everything seems to go off without a hitch. I'm trying to print the data retrieved to a label simply to see if everything worked correctly, but whenever I call my print method the app crashes. Could someone check my code and see if i'm missing something? I'm posting the relevent code below:

    //I'm creating a filename, converting the string (sProfessorName) to data aData and calling the writeApplicationData method

    NSString *fileName = @"professorInformation.data";
    NSData* aData;
    aData = [sProfessorName dataUsingEncoding: NSASCIIStringEncoding];
    [self writeApplicationData:aData toFile:fileName];
    NSString* aStr;






    }

    - (NSData *)applicationDataFromFileNSString *)fileName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
    return myData;
    }
    - (BOOL)writeApplicationDataNSData *)data toFileNSString *)fileName {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    if (!documentsDirectory) {
    NSLog(@"Documents directory not found!");
    return NO;
    }


    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
    return ([data writeToFile:appFile atomically:YES]);
    NSLog(@"%@ written to file", data);
    }



    //this is where everything gets screwy

    -(void)printProfessorid)sender{
    NSString *fileName = @"professorInformation.data";
    NSData *myData;
    NSString *aStr;
    [aSt appliDationDataFromFile:fileName];

    aStr = [[NSString alloc] initWithData:myData encoding:NSASCIIStringEncoding];
    label.text = aStr;


    }



  2. #2
    Rookie Array

    Join Date
    Feb 2008
    Posts
    12
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    0

    Default

    I decided to use a sqlite database, which worked...

  3. #3
    Senior Professional Array

    Join Date
    Jul 2007
    Posts
    151
    Post Thanks / Like
    Downloads
    0
    Uploads
    0
    Rep Power
    16

    Default

    Shouldn't it be
    -(void)printProfessorid)sender{
    NSString *fileName = @"professorInformation.data";
    NSData *myData = [self appliDationDataFromFile:fileName];
    NSString *aStr = [[NSString alloc] initWithData:myData encoding:NSASCIIStringEncoding];
    label.text = aStr;
    }

    ?

 

 

Similar Threads

  1. [Advice]2.0 to 2.0.1 without lost data
    By RYU81 in forum Winpwn
    Replies: 2
    Last Post: 08-14-2008, 07:27 PM
  2. [Advice] Iphone data usage?
    By thefewtheproud in forum General
    Replies: 1
    Last Post: 06-14-2008, 08:22 PM
  3. Replies: 2
    Last Post: 04-06-2008, 01:00 AM
  4. (Advice) Reading .plist files on windows
    By JHalton in forum Free Toolchain Software (Cydia App's)
    Replies: 1
    Last Post: 01-19-2008, 06:12 PM
  5. Replies: 3
    Last Post: 11-23-2007, 10:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Powered by vBulletin®
Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.
Search Engine Friendly URLs by vBSEO
(c) 2006-2012 Hackint0sh.org
All times are GMT +2. The time now is 02:09 PM.
twitter, follow us!