Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6255333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:20:47+00:00 2026-05-24T14:20:47+00:00

I’m a newbie iOS developer. I wrote a small application that save an NSMutableArray

  • 0

I’m a newbie iOS developer.
I wrote a small application that save an NSMutableArray array with my objects that derived from NSObject.
Application do the save but the file isn’t created in document directory and application can’t read.
this issue is both on the simulator and my iPhone 3gs 4.2.1

My NSMutableArray definition inside the appDelegate class:

@property (nonatomic,retain, readwrite) NSMutableArray *places;

My NSObject class:

 #import <Foundation/Foundation.h>


@interface Place : NSObject {
    NSString *name;
    NSString *location;
}

-(id) init:(NSString *)name: (NSString *)location;

@property (retain,nonatomic,readwrite) NSString *name;
@property (retain,nonatomic,readwrite) NSString *location;

@end

My StorageService library class:

 #import "StorageService.h"


@implementation StorageService

-(id) init {
    self = [super init];
    if (self != nil) {

    }
    return self;
}


-(void) saveArrayToFile:(NSString*) filename : (NSMutableArray *)arrayToSave{
    // get full path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fullPath = [paths objectAtIndex:0];
    fullPath = [fullPath stringByAppendingPathComponent:filename];  
    NSLog(@"Save in %@",fullPath);
    [arrayToSave writeToFile:fullPath atomically:YES];
}

-(NSMutableArray*) readArrayFromFile:(NSString *)filename {
    // get full path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *fullPath = [paths objectAtIndex:0];
    fullPath = [fullPath stringByAppendingPathComponent:filename];

    if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
        NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:fullPath];
        if (data == nil) {
            data = [[NSMutableArray alloc] init];
        }
        NSLog(@"Read from %@",fullPath);
        return data;
    } else {
        NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:fullPath];
        return data;
    }
}

-(void) dealloc {
    [super dealloc];
}

@end

and My functions in the appDelegate:

    -(void) saveApplicationData {
    [self.storageService saveArrayToFile : PLACES_FILE : self.places];
}

-(void) loadApplicationData {
    self.places = [self.storageService readArrayFromFile:PLACES_FILE];
}

Here is my class that holds constant to filename:

    #import <Foundation/Foundation.h>

extern NSString * const PLACES_FILE = @"Places.dat";

@interface ApplicationConstants : NSObject {

}

@end

So what is wrong?

Thank you guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:20:48+00:00Added an answer on May 24, 2026 at 2:20 pm

    What you want is to let Place conform to the NSCoding protocol, to allow for serialization to and from files (and in memory data if wanted)

    Extend Place as (I have also changed the name of the init method as your name was against every naming practice iOS has):

    @interface Place : NSObject <NSCoding> {
        NSString *name; 
        NSString *location; 
    }
    
    -(id)initWithName:(NSString *)name location:(NSString *)location;
    
    @property (retain,nonatomic,readwrite) NSString *name; 
    @property (retain,nonatomic,readwrite) NSString *location;
    
    @end
    

    Your implementation is quite simple but you also need to implement two methods defined by the NSCoding protocol:

    @implementation Place
    
    @synthesize name, location;
    
    -(id)initWithName:(NSString *)aName location:(NSString *)aLocation {
        self = [super init];
        if (self) {
            self.name = aName;
            self.location = aLocation;
        }
        return self;
    }
    
    -(id)initWithWithCoder:(NSCoder)decoder {
        self = [super initWithCoder:decoder];
        if (self) {
           self.name = [decoder decodeObjectForKey:@"name"];
           self.location = [decoder decodeObjectForKey:@"location";
        }
        return self;
    }
    
    -(void)encodeWithCoder:(NSCoder*)encoder {
        [encoder encodeObject:self.name forKey:@"name"];
        [encoder encodeObject:self.location forKey:@"location"];
        [super encodeWithCoder:encoder];
    }
    
    @end
    

    With this in place, saving the places array to disk is as easy as:

    [NSKeyedArchiver archiveRootObject:places toFile:path];
    

    And decoding just as easy:

    places = [[KSKeyUnarchiver unarchiveObjectWithFile:path] retain];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from
Seemingly simple, but I cannot find anything relevant on the web. What is the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.