Here's my solution. I've changed the name of your class to AppController, because it makes more sense (and starts with a capital letter!).
AppController.h:
Code:#import <Cocoa/Cocoa.h> @interface AppController : NSObject { IBOutlet NSTextField *textField; } - (IBAction)createTime:(id)sender; - (NSNumber *)longestRecordedUpTime; - (NSArray *)lastTenRecordedUpTimes; @end
AppController.m:
Code:#import "AppController.h" NSString *MBLongestRecordedUpTime = @"MBLongestRecordedUpTime"; NSString *MBLastTenRecordedUpTimes = @"MBLastTenRecordedUpTimes"; @interface NSNumber (MBUpTimeAdditions) + (id)currentUpTime; - (NSString *)formattedUpTimeString; @end @implementation AppController - (IBAction)createTime:(id)sender; { NSNumber *currentUpTime = [NSNumber currentUpTime]; [textField setStringValue:[currentUpTime formattedUpTimeString]]; if ([[NSUserDefaults standardUserDefaults] objectForKey:MBLongestRecordedUpTime] == nil || [currentUpTime compare:[[NSUserDefaults standardUserDefaults] objectForKey:MBLongestRecordedUpTime]] == NSOrderedDescending) [[NSUserDefaults standardUserDefaults] setObject:currentUpTime forKey:MBLongestRecordedUpTime]; NSMutableArray *lastTenUpTimes = [[[NSUserDefaults standardUserDefaults] objectForKey:MBLastTenRecordedUpTimes] mutableCopy]; if (lastTenUpTimes == nil) lastTenUpTimes = [[NSMutableArray alloc] initWithCapacity:10]; [lastTenUpTimes insertObject:currentUpTime atIndex:0]; if ([lastTenUpTimes count] > 10) [lastTenUpTimes removeLastObject]; [[NSUserDefaults standardUserDefaults] setObject:lastTenUpTimes forKey:MBLastTenRecordedUpTimes]; [lastTenUpTimes release]; } - (NSNumber *)longestRecordedUpTime; { return [[NSUserDefaults standardUserDefaults] objectForKey:MBLongestRecordedUpTime]; } - (NSArray *)lastTenRecordedUpTimes; { return [[NSUserDefaults standardUserDefaults] objectForKey:MBLastTenRecordedUpTimes]; } @end @implementation NSNumber (MBUpTimeAdditions) + (id)currentUpTime; { return [self numberWithUnsignedLongLong:UnsignedWideToUInt64(AbsoluteToNanoseconds(UpTime())) / 1000000000]; // UpTime in seconds } - (NSString *)formattedUpTimeString; { unsigned days = [self unsignedLongLongValue] / (60 * 60 * 24); unsigned hours = ([self unsignedLongLongValue] / (60 * 60)) % 24; unsigned minutes = ([self unsignedLongLongValue] / 60) % 60; return [NSString stringWithFormat:@"%ud %uh %um", days, hours, minutes]; } @end
thanks. i ran your code, and it compiled, but i don't think it works with our current user interface.
did you make your own user interface?