views:

244

answers:

3

I've been spending way too long trying to figure out what is going wrong, so I hope someone her can help me out.

My Code:

- (IBAction)fedDog {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"dogsFedDays.plist"];  
    NSMutableArray *dogsFedSave = [[NSMutableArray alloc] arrayWithCapacity: 48]; 
    for (int i = 0; i < 48; i++) { 
        NSDictionary *myDict = [[NSDictionary alloc] initWithObjectsAndKeys: 
           date[i], @"string",
           fed[i], @"Yes",
           nil]; 
        [dogsFedSave addObject:myDict]; 
        [myDict release]; 
    } 
    if (![dogsFedSave writeToFile:path atomically:YES]) 
        NSLog(@"not successful in completing this task"); 
}

I've connected the action to a button, but when the button is pressed, the simulator freezes, and no file appears in the Documents directory.

A: 

For starters, your action method should take a single argument as opposed to none. Beyond that, have you run it in the debugger?

Azeem.Butt
IBActions can have one argument, or none. As far as I know both are valid.
Kenny Winker
A: 

It's possible that the simulator freezes because of an unhandled exception. Check the debug console while your app is running. The most probable reason your application hangs is that the button pressed may be calling fedDog: method. Try changing your method signature to this one:

- (IBAction)fedDog:(id)sender {
    // your code
}
Alex
the whole purpose is that fedDog: is being called, no? anyway, the wrong signature would not actually cause an exception.
hop
@hop: well, actually, it would. It would raise an exception "unrecognized selector sent to instance ...". Only the explicit check whether the given instance responds to either fedDog or fedDog: would prevent the exception from being thrown. Although, in this particular case your point about arrayWithCapacity is a more probable cause of the problem. I haven't noticed it earlier.
Alex
sorry, i missed the colon there, last night! you are right, of course!
hop
A: 

You are trying to send arrayWithCapacity to an instance of NSMutableArray, but it is actually declared as

+ (id)arrayWithCapacity:(NSUInteger)numItems

and hence a class method.

So either use

NSMutableArray *dogsFedSave = [[NSMutableArray alloc] initWithCapacity:48];

or

NSMutableArray *dogsFedSave = [NSMutableArray arrayWithCapacity:48];

The latter would be better, since it results in an autoreleased object (and you forgot to release dogsFedSave anyway…)

hop
Ok. I put "+ (id)arrayWithCapacity:(NSUInteger)numItems " in the header file. One thing I noticed, (which I don't think the new code caused) is that there are 4 warnings at the bottom of the classes file. 1) incomplete implementation of class "feeddogviewcontroller" 2)method definition for +arraywithcapacity not found 3)incomplete implementation of class feeddogviewcontroller (again) 4)method definition for -fedDog not found . If you have any idea as to why I am getting those warnings, please let me know. Thanks!
Yogi
why would you do that???
hop
I think I'm a little confused. Where's the "+ (id)arrayWithCapacity:(NSUInteger)numItems " supposed to go?
Yogi
nowhere. i said "it is declared" not "you have to declare", didn't i?
hop
sorry, that was my mistake. Anyway, I changed the line, "NSMutableArray *dogsFedSave = [[NSMutableArray alloc] arrayWithCapacity: 48]; " to "NSMutableArray *dogsFedSave = [NSMutableArray arrayWithCapacity:48]; " though the plist is still not created, and I continue to get two of the warnings at the end of the .m file ( 1)method definition for -fedDog not found 2) incomplete implementation of class "feeddogviewcontroller"). Any ideas?
Yogi
there is not enough context to debug those other problems
hop
[Session started at 2009-12-28 17:02:50 -0500.]2009-12-28 17:03:39.333 FeedDog[232:20b] *** -[FeedDogViewController fedDog]: unrecognized selector sent to instance 0x3d1d9702009-12-28 17:03:39.367 FeedDog[232:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[FeedDogViewController fedDog]: unrecognized selector sent to instance 0x3d1d970'2009-12-28 17:03:39.387 FeedDog[232:20b] Stack: ( 8307803, 2494025275, 8689723, 8259190, 8111810, 23589977, 23997346, 24006083,
Yogi
24001295, 23694899, 23603228, 23630005, 126673, 126790, 8091873, 8088648, 120717, 120914, 23633923, 9188, 9042 ) ^That was copied from the console. It all results from pressing the button that is connected to the action. Does this help at all?
Yogi
please, don't do this in the comments.
hop