2011-03-30 5 views
0

plist 파일을 만들고 데이터를 쓰고 아래 코드를 사용하여 데이터를 읽으려고하는데 어떻게 든 앱이 다운됩니다. 문자열 값을 저장하려고합니다. 사람이 맞다까지 우리가 작업을 위해, 당신은 충분한 데이터를 제공하지 않았다 내가 얻을plist 파일 도움말

//PLIST FILE START.. 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1 
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; //3 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if (![fileManager fileExistsAtPath: path]) //4 
    { 
     NSString *bundle = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //5 

     [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6 
    } 

    //And write data: 

    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

    //here add elements to data file and write data to file 
    //NSString *value = rowValue; 

    [data setObject:[NSString stringWithFormat:@"%@", rowValue] forKey:@"value"]; 

    [data writeToFile: path atomically:YES]; 
    [data release]; 

    //Read Data.. 
    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path]; 

    //load from savedStock example int value 
    NSString *test; 
    test = [savedStock objectForKey:@"value"]; 

    [savedStock release]; 

    NSLog(@"PLIST VALUE:%@",test); 
    //PLIST FILE END.. 

오류,

 
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString stringValue]: unrecognized selector sent to instance 0x4b25af0' 
*** Call stack at first throw: 
(
0 CoreFoundation      0x00dd9be9 __exceptionPreprocess + 185 
1 libobjc.A.dylib      0x00f2e5c2 objc_exception_throw + 47 
2 CoreFoundation      0x00ddb6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
3 CoreFoundation      0x00d4b366 ___forwarding___ + 966 
4 CoreFoundation      0x00d4af22 _CF_forwarding_prep_0 + 50 
5 Gavel Edge       0x00003e1e -[JobViewController tableView:didSelectRowAtIndexPath:] + 832 
6 UIKit        0x00358794 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140 
7 UIKit        0x0034ed50 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219 
8 Foundation       0x000617f6 __NSFireDelayedPerform + 441 
9 CoreFoundation      0x00dbafe3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19 
10 CoreFoundation      0x00dbc594 __CFRunLoopDoTimer + 1220 
11 CoreFoundation      0x00d18cc9 __CFRunLoopRun + 1817 
12 CoreFoundation      0x00d18240 CFRunLoopRunSpecific + 208 
13 CoreFoundation      0x00d18161 CFRunLoopRunInMode + 97 
14 GraphicsServices     0x0170e268 GSEventRunModal + 217 
15 GraphicsServices     0x0170e32d GSEventRun + 115 
16 UIKit        0x002f142e UIApplicationMain + 1160 
17 Gavel Edge       0x00001de4 main + 102 
18 Gavel Edge       0x00001d75 start + 53 
19 ???         0x00000001 0x0 + 1 
) 
terminate called after throwing an instance of 'NSException'
+0

"어떻게 든 앱이 다운됩니다"라는 표현은 전혀 도움이되지 않습니다. 디버거를 사용해 보시고 그 어플리케이션이 충돌하고있는 지점을 정확하게 찾으시기 바랍니다. – Till

+0

어떤 라인이 충돌합니까? 어떤 오류가 있습니까? – GoatInTheMachine

+0

죄송합니다. 코드를 편집하여 오류를 추가했습니다. – developer

답변

2

그냥이 이미있는 NSString 객체입니다

test = [savedStock objectForKey:@"value"]; 

사용 ..과에는 기능 "stringValue"는 존재하지 않는다.

+0

고마워요 @ 바스 티앙. 나는 그것을했고 지금은 나에게 어떤 오류도주지 않을 것이다. 그러나 그것은 또한 줄을 인쇄하지 않을 것이다. NSLog (@ "PLIST VALUE : % @", test); 내가 할 필요가있는 다른 무엇이 있습니까? – developer

+0

로드 후 NSLog (@ "dict % @", savedStock)로 전체 사전을 인쇄 해보십시오 ... 그게 효과가 있습니까? – Bastian

+0

예. 작동 중입니다. 아래 출력을 얻고 있습니다. { stringVal = test; 값 = TST1101; } – developer

1

이 좀 도와 주시겠습니까. 하지만 좋은 아침을 보내고있어 어둠 속에서 몇 번씩 찌를거야. 그것이 휴식까지 코드를 통해

// Maybe a path wasn't found? 
NSString *documentsDirectory = [paths objectAtIndex:0]; 

// Maybe the resource wasn't found? 
[fileManager copyItemAtPath:bundle toPath: path error:&error]; 

단계 : 나는이 라인을 확인한다. 오류가있는 위치가 표시됩니다. 대신

test = [[savedStock objectForKey:@"value"] stringValue]; 

+0

죄송합니다. 코드를 편집하여 오류를 추가했습니다. – developer

+0

나는 초보자이므로 http://ipgames.wordpress.com/tutorials/writeread-data-to-plist-file/ url에 제공된 코드를 사용했습니다. 코드에서 언급 한대로 프로젝트에서 plist 파일을 만들었습니다. 내가 잘못한 것을 알려주고 도와 주셔서 고맙습니다. – developer

+0

인식 할 수없는 선택자는 코드 라인의 첫 번째 부분이 유효하지 않다는 것을 의미합니다. 다음 줄을보십시오 : [[savedStock objectForKey : @ "value"] stringValue]; [savedStock objectForKey : @ "value"] <-이 부분은 아마도 범인입니다. –