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 1007765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:40:03+00:00 2026-05-16T08:40:03+00:00

I’m going crazy with this my little app… Please help me!!! this is the

  • 0

I’m going crazy with this my little app… Please help me!!!

this is the source code of the app: Smoking.zip

It only saves a .dat file with an NSMutableArray.
Now, the first time you will launch the app, try to click the cigarette button sometimes: Everything should working fine.
Ok, now close the app, re-open it, and click again on the button. This time the app will crash with the “unrecognized selector sent to instance 0x5d18d60” error.
I was sure the problem was in saving the data, because when i commented the line “[theData writeToFile:dataFilePath atomically:YES];” in the “saveData” method the error disappeared.
Later i discovered that it appears again if i try to read the data from the NSMutableArray.

Please take a moment to check my project and help me, beacause i’m going crazy about that!!

Here’s some code:

#import "SmokingAppDelegate.h"
#import "SmokingViewController.h"
#import "Cig.h"

@implementation SmokingAppDelegate

@synthesize window;
@synthesize viewController, dataFilePath, smokeArray;


#pragma mark -
#pragma mark Application lifecycle

- (id) init {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"a.dat"];

    [self setDataFilePath:path];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if([fileManager fileExistsAtPath:dataFilePath]

       ) {
        //open it and read it 
        NSLog(@"data file found. reading into memory");

        smokeArray = [[NSMutableArray alloc] init];

        NSMutableData *theData;
        NSKeyedUnarchiver *decoder;
        NSMutableArray *tempArray;

        theData = [NSData dataWithContentsOfFile:dataFilePath];
        decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:theData];
        tempArray = [decoder decodeObjectForKey:@"smokeArray"];
        [self setSmokeArray:tempArray];

        [decoder finishDecoding];
        [decoder release];      
    } else {
        NSLog(@"no file found. creating empty array");

        smokeArray = [[NSMutableArray alloc] init];
        [smokeArray insertObject:[[NSNumber alloc] initWithInt:0] atIndex:0];

    }

//  [self logArrayContents];

    return self;
}

- (void) logArrayContents {

    for(int j = 1; j < [smokeArray count]; j++) {
        int f = [[[smokeArray objectAtIndex:j] num] intValue];
        NSLog(@"%i. - %d", j, f);
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}



-(void) saveData {
    NSMutableData *theData;
    NSKeyedArchiver *encoder;

    theData = [NSMutableData data];
    encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData];

    [encoder encodeObject:smokeArray forKey:@"smokeArray"];

    [encoder finishEncoding];

    [theData writeToFile:dataFilePath atomically:YES];
    [encoder release];
    NSLog(@"Saved");
}

#pragma mark -
#pragma mark Memory management


- (void)dealloc {
    [viewController release];
    [window release];
    [dataFilePath release];
    [smokeArray release];
    [super dealloc];
}


@end



#import "SmokingViewController.h"
#import "SmokingAppDelegate.h"
#import "Cig.h"

@implementation SmokingViewController
@synthesize label;

- (void)viewDidLoad {
[super viewDidLoad];

    SmokingAppDelegate *mainDelegate = (SmokingAppDelegate *)[[UIApplication sharedApplication] delegate];

//controlla se il giorno è lo stesso rispetto a quello dell'ultima sigaretta fumata
    if ([mainDelegate.smokeArray count] > 1) {


        Cig *oldCig = [mainDelegate.smokeArray lastObject];
        NSArray *tempArray = [self quando];

        if (    [[tempArray objectAtIndex:0] intValue]==[[oldCig.dat objectAtIndex:0] intValue]
            &&  [[tempArray objectAtIndex:1] intValue]==[[oldCig.dat objectAtIndex:1] intValue]
            &&  [[tempArray objectAtIndex:2] intValue]==[[oldCig.dat objectAtIndex:2] intValue]
            ) {
            N = [oldCig.num intValue];
        }
        else {
            N = 0;
        }

        [oldCig release];
        [tempArray release];

 }


//scrive quante sigarette si sono fumate oggi
    label.text = [NSString stringWithFormat: @"Today you smoked %d cigarettes",N];




}

- (IBAction) smoke:(UIButton * ) button {

    SmokingAppDelegate *mainDelegate = (SmokingAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSLog(@"L'array contiene %d sigarette", [mainDelegate.smokeArray count]-1);

    N += 1;

    [self addNewCigToArray];
    [mainDelegate logArrayContents];
    [mainDelegate saveData];

    label.text = [NSString stringWithFormat: @"Today you smoked %d cigarettes",N];


}


- (void) addNewCigToArray {

    //NSLog(@"new cigarette smoked");
    SmokingAppDelegate *mainDelegate = (SmokingAppDelegate *)[[UIApplication sharedApplication] delegate];
    Cig *newCig = [[Cig alloc] init]; 

    [newCig setDat:[self quando]]; 
    [newCig setNum:[[NSNumber alloc] initWithInt:N]];

    [mainDelegate.smokeArray addObject:newCig]; 
    [newCig release];
    //[mainDelegate logArrayContents];

}

- (NSArray *) quando {

    NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];   

    // 0 - Year
    [timeFormat setDateFormat:@"YYYY"];
    NSString *year = [timeFormat stringFromDate:[NSDate date]];

    // 1 - Month
    [timeFormat setDateFormat:@"MM"];
    NSString *month = [timeFormat stringFromDate:[NSDate date]];

    // 2 - Day
    [timeFormat setDateFormat:@"dd"];
    NSString *day = [timeFormat stringFromDate:[NSDate date]];

    // 3 - Hour 
    [timeFormat setDateFormat:@"HH"];
    NSString *hour = [timeFormat stringFromDate:[NSDate date]];

    // 4 - Minute
    [timeFormat setDateFormat:@"mm"];
    NSString *min = [timeFormat stringFromDate:[NSDate date]];

    // 5 - Second
    [timeFormat setDateFormat:@"ss"];
    NSString *sec = [timeFormat stringFromDate:[NSDate date]];

    NSArray *newArray = [[NSArray alloc] initWithObjects:year,month,day,hour,min,sec,nil];

    return newArray;

}


- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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

@end
  • 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-16T08:40:04+00:00Added an answer on May 16, 2026 at 8:40 am

    Okay, I am an iPhone newbie so take my suggestion in stride. You could try creating an NSData object, initializing it with theData, and then calling writeToFile on the new NSData object instead of the NSMutableData object.

    • 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
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

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.