2010-08-08 5 views
0

나는이 작은 앱으로 미쳐 갈 것입니다 ... 제발 도와주세요 !!! Smoking.zip인식 할 수없는 선택기가 인스턴스로 전송되었습니다 ... 내부 소스 코드 링크

그것은 단지 NSMutableArray를 가진 .DAT 파일을 저장합니다 :

앱의 소스 코드입니다. 이제 처음으로 앱을 시작할 때 담배 버튼을 클릭하십시오. 모든 것이 잘 작동합니다. 이제 앱을 닫은 다음 다시 열고 버튼을 다시 클릭하십시오. 이번에는 앱이 "인스턴스 0x5d18d60으로 전송 된 인식 할 수없는 선택자"오류로 인해 충돌합니다. "[theData writeToFile : dataFilePath atomically : YES]; 행을 주석 처리했기 때문에 문제가 데이터 저장에 있다고 확신했습니다." "saveData"메서드에서 오류가 사라졌습니다. 나중에 NSMutableArray에서 데이터를 읽으려고하면 다시 나타납니다.

잠시 시간을내어 프로젝트를 확인하고 저를 도와주세요. beacause에 대해 미쳐 가고 있습니다 !! 나는 아이폰 초보자 그렇게 걸음에 내 제안을 받아 오전

#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 
+0

Dupe : http://stackoverflow.com/questions/3436007/unrecognized-selector-sent-to-instance-0x5d18d60-im-going-crazy –

+2

예, 새로 추가되었습니다. 아니요, 귀하가 질문을 다시하지 않으려 고하지 않을 것입니다. –

+0

죄송합니다. 그러나 아무도 저에게 응답하고 있지 않으며, 나는 주된 이유가 1 개의 대답을 받았다고 생각했습니다. 하지만 그 대답은 정확하지 않았습니다 ... 희망 누군가가 나를 도울 것입니다! – Abramodj

답변

0

좋아요 :

는 여기에 몇 가지 코드입니다. NSData 객체를 만들어서 theData로 초기화 한 다음 NSMutableData 객체 대신 새 NSData 객체에서 writeToFile을 호출 해보십시오.

관련 문제