iOS 응용 프로그램 에서 사용자 가 설정 한 plist 파일 의 생 성 및 읽 기 및 쓰기 튜 토리 얼 을 저장 합 니 다.

3720 단어 iOSplist
iOS 개발 을 할 때 plist 파일 에 자주 사용 되 는데,  그럼 plist 파일 은 무엇 입 니까?전체 이름 은 property List,속성 목록 파일 입 니 다.직렬 화 된 대상 을 저장 하 는 파일 입 니 다.속성 목록 파일 의 확장 자 는.plist 이기 때문에 보통 plist 파일 이 라 고 합 니 다.파일 은 xml 형식 입 니 다.
Plist 파일 은 보통 사용자 설정 을 저장 하 는 데 사용 되 며,묶 인 정 보 를 저장 하 는 데 도 사용 된다.
우 리 는 plist 파일 의 읽 기와 쓰 기 를 배 울 항목 을 만 들 었 다.
1.프로젝트 Plistdemo 만 들 기
프로젝트 생 성 후 프로젝트 에 대응 하 는 plist 파일 을 찾 을 수 있 습 니 다.다음 그림 과 같이 엽 니 다.
201641890318772.png (611×296)
편집기 에 표 와 유사 한 형식 을 표시 합 니 다.plist 에서 오른쪽 단 추 를 누 르 고 소스 코드 로 열 면 plist 파일 의 xml 형식 을 볼 수 있 습 니 다.
2.plist 파일 을 만 듭 니 다.
command+N 단축 키 로 만 들 거나 File―>New―>New File 을 누 르 고 Mac OS X 의 Property List 를 선택 하 십시오.
201641890447160.jpg (728×491)
파일 이름 은 custom Info 이 고 Group 은 Supporting Files 를 선택 합 니 다.
3.새로 만 든 customeInfo.plist 를 누 르 면 데 이 터 를 추가 합 니 다.다음 그림:
201641890508538.png (417×150)
Type 항목 의 종 류 는 Dictionary 를 선택 하고 Source Code 로 열 어 다음 과 같이 표시 합 니 다.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
 <key>Student</key>
 <dict>
 <key>Name</key>
 <string>Yang</string>
 <key>Sex</key>
 <string>Male</string>
 <key>Num</key>
 <string>SX_010</string>
 </dict>
 <key>Mentor</key>
 <dict>
 <key>Name</key>
 <string>Gu</string>
 <key>Sex</key>
 <string>Male</string>
 </dict>
</dict>
</plist>
4.보기 에 컨트롤 추가:
BIDViewController.xib 를 누 르 면 IB 를 열 고 몇 개의 컨트롤 을 끌 어 올 리 고 레이아웃 을 설정 합 니 다.다음 그림:
201641890555870.png (442×571)
위의 그림 에서 모든 컨트롤 은 Label 이 고 글꼴 크기 를 설정 합 니 다.
5.다음은 맵 입 니 다.다섯 개의 회색 레이 블 을 모두 BIDViewController.h 파일 에 투사 합 니 다.유형 은 모두 Outlet 이 고 이름 은 stuName,stuSex,stuNum,mtName,mtSex 입 니 다.
6.BIDViewController.m 을 누 르 면 viewdLoad 방법 중의[슈퍼 viewdLoad]에 다음 코드 를 추가 합 니 다.

// studentInfo.plist
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"customInfo" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
   
//
NSDictionary *tmpInfo = [dictionary objectForKey: @"Student"];
self.stuName.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Name"]];
self.stuSex.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Sex"]];
self.stuNum.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Num"]];
   
//
tmpInfo = [dictionary objectForKey: @"Mentor"];
self.mtName.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Name"]];
self.mtSex.text = [NSString stringWithFormat:@"%@", [tmpInfo objectForKey: @"Sex"]];
7.실행,효과 보기:
201641890616043.png (444×573)

좋은 웹페이지 즐겨찾기