Need help loading file

Discussion in 'iOS Programming' started by shknerth, Sep 6, 2010.

  1. shknerth macrumors newbie

    Joined:
    Feb 17, 2010
    #1
    So I'm trying to make a simple app to give me reminders to do things. I've been trying to make it so it saves and loads reminders. It hasn't been working and its been giving me a long exception. I can't figure out why so I came here to ask for help. If you need the other classes, don't hesitate to ask.

    Code:
    //  Reminders
    //
    
    
    #import <UIKit/UIKit.h>
    #import "Reminder.h"
    #import "DetailViewController.h"
    
    @interface RootViewController : UITableViewController {
    	
    	NSMutableArray *reminders;
    	NSMutableString *currentTitle;
    	NSMutableString *currentDate;
    	NSMutableString *currentSummary;
    	NSMutableString *currentLink;
    	
    	
    }
    
    - (IBAction) addReminder;
    - (void) addNewReminder:(Reminder *)reminder;
    -(NSString *) pathOfFile;
    -(void) saveData;
    
    @property(nonatomic, retain) IBOutlet UITableViewCell *cell;
    @property (nonatomic, retain) NSMutableArray *reminders;
    
    @end
    
    
    Code:
    //
    //  RootViewController.m
    //  Reminders
    
    
    #import "RootViewController.h"
    #import "DetailViewController.h"
    #import "AddReminderViewController.h"
    
    
    @implementation RootViewController
    
    @synthesize cell, reminders;
    
    
    #pragma mark -
    #pragma mark View lifecycle
    
    
    - (void)viewDidLoad {
    	[super viewDidLoad];
    	
    	reminders = [[NSMutableArray alloc] init];
    	currentTitle = [[NSMutableString alloc] init];
    	currentDate = [[NSMutableString alloc] init];
    	currentSummary = [[NSMutableString alloc] init];
    	currentLink = [[NSMutableString alloc] init];
       
    	
    	UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
    																			   target:self 
    																			  action:@selector(addReminder)];
    	
    	self.navigationItem.leftBarButtonItem = addButton;
    
        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        self.navigationItem.rightBarButtonItem = self.editButtonItem;
    	
    	NSString *filePath = [self pathOfFile];
    	
    	
    	@try{
    	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
    		NSLog(@"Hello");
    		
    		NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
    			[reminders addObjectsFromArray:array];
    			NSLog(@"%@", [array objectAtIndex:0]);
    				[array release];
    		
    			
    		
    		
    		
    		
    		NSLog(@"PVC: %@", [reminders description]);
    		
    			
    		
    	
    		
    		NSLog(@"PVC: %@",[self.tableView description]);
    		
    		NSLog(@"It's here");
    	}
    	
    	UIApplication *app = [UIApplication sharedApplication];
    	[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:)
    												name:UIApplicationWillTerminateNotification object:app];
    	
    NSLog(@"Loaded");
    	
    	} @catch (NSException *e) {
    		NSLog(@"don't do it!!!!!!!");
    	}
    
    }
    
    - (void)scheduleNotificationWithItem:(Reminder *)item interval:(int)minutesBefore {
    
        NSDate *itemDate = item.date;
    	
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        if (localNotif == nil)
            return;
        localNotif.fireDate = [itemDate addTimeInterval:-(minutesBefore*60)];
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
    	
        localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"%@ in %i minutes.", nil),
    							item.name, minutesBefore];
        localNotif.alertAction = NSLocalizedString(@"View Details", nil);
    	
        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;
    	
        NSDictionary *infoDict = [NSDictionary dictionaryWithObject:item.name forKey:@"reminderItem"];
        localNotif.userInfo = infoDict;
    	
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
        [localNotif release];
    }
    
    
    - (void) addNewReminder:(Reminder *)reminder {
    	NSLog(@"Recieved reminder to add");
    	
    	[self scheduleNotificationWithItem:reminder interval:5];
    	
    	[reminders addObject:reminder];
    	
    	[self.tableView reloadData];
    }
    
    /*
    - (void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
    }
    */
    
    - (void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
    	[self.tableView reloadData];
    }
    
    /*
    - (void)viewWillDisappear:(BOOL)animated {
    	[super viewWillDisappear:animated];
    }
    */
    /*
    - (void)viewDidDisappear:(BOOL)animated {
    	[super viewDidDisappear:animated];
    }
    */
    
    /*
     // Override to allow orientations other than the default portrait orientation.
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	// Return YES for supported orientations.
    	return YES;
    }
     */
    
    
    #pragma mark -
    #pragma mark Table view data source
    
    // Customize the number of sections in the table view.
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }
    
    
    // Customize the number of rows in the table view.
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [reminders count];
    }
    
    
    // Customize the appearance of table view cells.
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        
    	cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        }
        
    	// Configure the cell.
    	
    	Reminder *theReminder = [reminders objectAtIndex:indexPath.row];
    	
    	cell.textLabel.text = theReminder.name;
    	
        return cell;
    	
        }
    
    
    /*
    // Override to support conditional editing of the table view.
    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the specified item to be editable.
        return YES;
    }
    */
    
    
    
    // Override to support editing the table view.
    - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
        
        if (editingStyle == UITableViewCellEditingStyleDelete) {
            // Delete the row from the data source.
    		[reminders removeObjectAtIndex:indexPath.row];
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }   
        else if (editingStyle == UITableViewCellEditingStyleInsert) {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
        }   
    }
    
    
    
    /*
    // Override to support rearranging the table view.
    - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    }
    */
    
    
    /*
    // Override to support conditional rearranging of the table view.
    - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
        // Return NO if you do not want the item to be re-orderable.
        return YES;
    }
    */
    
    
    #pragma mark -
    #pragma mark Table view delegate
    
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        
    	
    	 DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
         // ...
         // Pass the selected object to the new view controller.
    	Reminder *theReminder = [reminders objectAtIndex:indexPath.row];
    
    	detailViewController.reminder = theReminder;
    	
    	
    	 [self.navigationController pushViewController:detailViewController animated:YES];
    	 [detailViewController release];
    	
    }
    
    
    #pragma mark -
    #pragma mark Memory management
    
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Relinquish ownership any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
        // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
        // For example: self.myOutlet = nil;
    }
    
    
    
    
    /*- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	// Return YES for supported orientations
    	//return (interfaceOrientation == UIInterfaceOrientationPortrait);
    	return YES;
    }
    */
    
    - (IBAction) addReminder{
    	NSLog(@"Add new reminder");
    	AddReminderViewController *addReminderViewController = [[AddReminderViewController alloc] initWithNibName:@"AddReminderViewController" bundle:nil];
    	// ...
    	// Pass the selected object to the new view controller.
    	//Reminder *theReminder = [reminders objectAtIndex:indexPath.row];
    	
    	//addReminderViewController.reminder = theReminder;
    	
    	
    	[self.navigationController pushViewController:addReminderViewController animated:YES];
    	[addReminderViewController release];
    	
    }
    
    -(NSString *) pathOfFile{
    
    	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    	NSString *documentFolder = [paths objectAtIndex:0];
    	return [documentFolder stringByAppendingFormat:@"SavedReminders.plist"];
    
    }
    
    
    -(void) saveData {
    
    	NSMutableArray *array = [[NSMutableArray alloc]init];
    	[array addObject:reminders];
    	[array writeToFile:[self pathOfFile] atomically:YES];
    	NSLog(@"%@", [self pathOfFile]);
    	[array release];
    
    
    }
    
    
    - (void)dealloc {
        [super dealloc];
    }
    
    
    @end
    
    
    
    Exception:
    Code:
    [B][Session started at 2010-09-06 14:54:59 -0400.]
    2010-09-06 14:55:00.778 Reminders[14108:207] ******* Accessibility Status Changed: On
    2010-09-06 14:55:00.806 Reminders[14108:207] ********** Loading AX for: com.yourcompany.Reminders ************
    2010-09-06 14:55:00.856 Reminders[14108:207] Hello
    2010-09-06 14:55:00.857 Reminders[14108:207] (
    )
    2010-09-06 14:55:00.858 Reminders[14108:207] PVC: (
            (
        )
    )
    2010-09-06 14:55:00.859 Reminders[14108:207] PVC: <UITableView: 0x702c400; frame = (0 20; 320 460); clipsToBounds = YES; opaque = NO; autoresize = W+H; layer = <CALayer: 0x693a0f0>; contentOffset: {0, 0}>
    2010-09-06 14:55:00.860 Reminders[14108:207] It's here
    2010-09-06 14:55:00.861 Reminders[14108:207] Loaded
    2010-09-06 14:55:00.866 Reminders[14108:207] -[__NSCFArray name]: unrecognized selector sent to instance 0x693bc80
    2010-09-06 14:55:00.868 Reminders[14108:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray name]: unrecognized selector sent to instance 0x693bc80'
    *** Call stack at first throw:
    (
    	0   CoreFoundation                      0x0248bb99 __exceptionPreprocess + 185
    	1   libobjc.A.dylib                     0x025db40e objc_exception_throw + 47
    	2   CoreFoundation                      0x0248d6ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    	3   CoreFoundation                      0x023fd2b6 ___forwarding___ + 966
    	4   CoreFoundation                      0x023fce72 _CF_forwarding_prep_0 + 50
    	5   Reminders                           0x00003ab1 -[RootViewController tableView:cellForRowAtIndexPath:] + 300
    	6   UIKit                               0x0032ed6f -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:withIndexPath:] + 619
    	7   UIKit                               0x00324e02 -[UITableView(UITableViewInternal) _createPreparedCellForGlobalRow:] + 75
    	8   UIKit                               0x00339774 -[UITableView(_UITableViewPrivate) _updateVisibleCellsNow:] + 1561
    	9   UIKit                               0x003317ec -[UITableView layoutSubviews] + 242
    	10  QuartzCore                          0x04569481 -[CALayer layoutSublayers] + 177
    	11  QuartzCore                          0x045691b1 CALayerLayoutIfNeeded + 220
    	12  QuartzCore                          0x045622e0 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 302
    	13  QuartzCore                          0x04562040 _ZN2CA11Transaction6commitEv + 292
    	14  UIKit                               0x002c004e -[UIApplication _reportAppLaunchFinished] + 39
    	15  UIKit                               0x002c0477 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 545
    	16  UIKit                               0x002ca3ec -[UIApplication handleEvent:withNewEvent:] + 1958
    	17  UIKit                               0x002c2b3c -[UIApplication sendEvent:] + 71
    	18  UIKit                               0x002c79bf _UIApplicationHandleEvent + 7672
    	19  GraphicsServices                    0x02d6b822 PurpleEventCallback + 1550
    	20  CoreFoundation                      0x0246cff4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    	21  CoreFoundation                      0x023cd807 __CFRunLoopDoSource1 + 215
    	22  CoreFoundation                      0x023caa93 __CFRunLoopRun + 979
    	23  CoreFoundation                      0x023ca350 CFRunLoopRunSpecific + 208
    	24  CoreFoundation                      0x023ca271 CFRunLoopRunInMode + 97
    	25  UIKit                               0x002bfc6d -[UIApplication _run] + 625
    	26  UIKit                               0x002cbaf2 UIApplicationMain + 1160
    	27  Reminders                           0x00002080 main + 102
    	28  Reminders                           0x00002011 start + 53
    )
    terminate called after throwing an instance of 'NSException'[/B]
    
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. shknerth thread starter macrumors newbie

    Joined:
    Feb 17, 2010
    #3
    Sorry about that. It's there now.
     

Share This Page