2013-02-06 1 views
0

해당 키 - 값 쌍을 사용하여 plist에 TextField의 내용을 저장하려고합니다. 비밀번호 입력란은 Key-Password 및 Value- (textField에 입력)와 함께 저장해야합니다.TextField의 내용을 plist로 저장하는 방법

어떻게하면됩니까?

다른 클래스에서 액세스하고 싶습니다. 할 수 있을까요? 그렇다면 어떻게?

다음과 같은 방식으로 PLIST에에 textField의 내용을 저장할 수 있습니다

답변

1

plist에 물건을 추가하는 것은 쉽습니다. 전체 작업 코드는 사람이 PLIST에 연락처 정보를 추가하는 다음 -

// get paths from root direcory 
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
// get documents path 
NSString *documentsPath = [paths objectAtIndex:0]; 
// get the path to our Data/plist file 
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"]; 

// set the variables to the values in the text fields 
self.personName = nameEntered.text; 
self.phoneNumbers = [[NSMutableArray alloc] initWithCapacity:3]; 
[phoneNumbers addObject:homePhone.text]; 
[phoneNumbers addObject:workPhone.text]; 
[phoneNumbers addObject:cellPhone.text]; 

// create dictionary with values in UITextFields 
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: personName, phoneNumbers, nil] forKeys:[NSArray arrayWithObjects: @"Name", @"Phones", nil]]; 

NSString *error = nil; 
// create NSData from dictionary 
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 

// check is plistData exists 
if(plistData) 
{ 
    // write plistData to our Data.plist file 
    [plistData writeToFile:plistPath atomically:YES]; 
} 
else 
{ 
    NSLog(@"Error in saveData: %@", error); 
    [error release]; 
} 

[source]

+0

안녕하세요, 감사합니다 ..하지만 내 PLIST가 저장되는 위치 (장소)를받지 메신저 ... 도서관 내 로컬에 존재하지 않는 계정 .. 검색하려면 어떻게해야합니까? i hav는 Finder에서도 검색했습니다 .. –

+0

여기에 하위 폴더에 있다고 생각합니다 ~/Library/Application Support/iPhone Simulator/User/Applications /'more here - http://mobiledevelopertips.com/xcode/where-does -xcode-simulator-write-files.html –

+0

라이브러리 폴더를 가져 오지 못했습니다. 해당 경로를 표시하지만 주어진 경로를 추적 할 수 없습니다. –

0

: 당신이 PLIST에서 해당 값을 검색하려면

 NSMutableDictionary *_plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:pListPath]; 
     [_plistDict setValue:textField.text forKey:@"Password"]; 
     [_plistDict writeToFile:pListPath atomically:YES]; 

는, 다음과 같은 코드를 사용할 수 있습니다

NSMutableDictionary *_plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:pListPath]; 
    NSString *status = [NSString stringWithFormat:@"%@",[_plistDict objectForKey:@"Password"]]; 
0

을 예 NSMutableDictionary의 값을 속성의 text에 추가하여 사전의 키에 입력하십시오. Like

  NSMutableDicitonary * dictionary = [[NSMutableDictionary alloc] init]; 
     [dictionary setObject:textfield.text forKey:@"username"]; 
     [dictionary setObject:textfield.text forKey:@"password"]; 

사전을 plist에 저장하고 필요한 곳에 사용하십시오.

0
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 

NSString *aFilePath = [NSString stringWithFormat:@"%@/plistName.plist", aDocumentsDirectory]; 

NSMutableArray *plistArray = [[NSMutableArray alloc] initWithContentsOfFile:aFilePath]; 


NSMutableDictionary *newComment = [NSMutableDictionary dictionary]; 
[newComment setValue:userName.text forKey:@"username"]; 
[newComment setValue:password.text forKey:@"password"]; 


[plistArray addObject:newComment]; 
[plistArray writeToFile:filePath atomically:YES]; 
0

모범 사례 대신 PLIST의 NSUserDefaults에서 이러한 정보를 저장하는 것입니다. NSUserDefautls는 프로젝트의 어느 곳에서나 액세스 할 수 있습니다.

저장 :

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
[userDefaults setObject:[yourTextfield text] forKey:@"password"]; 

을 찾는 중 :

NSString *myPassword = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"]; 
+0

다른 방법으로 NSUserDefaults에 액세스하는 것이 가장 좋은 방법 일 수 있습니까? 클래스 가져 오기를 시도했지만 오류가 발생했습니다. –

+0

'[NSUserDefaults standardUserDefaults]'는 싱글 톤이므로 앱의 어느 곳에서나 액세스 할 수 있습니다. 아무것도 가져올 필요가 없습니다. ViewController간에 객체를 전달할 필요가 없습니다. 한 곳에서 저장하고 다른 곳에서 액세스하십시오. – sleepwalkerfx

+0

메신저에 변수가 선언되지 않았다는 오류가 발생합니다 .. –

관련 문제