iOS
NSPredicate를 통한 쿼리 및 iOS 앱 개발 팁
11 분
nspredicate로 쿼리하기 소개 이 섹션에서는 objective c에서 쿼리를 정의하기 위해 nspredicate를 사용하는 방법을 배웁니다 언제든지 이 튜토리얼로 구축된 전체 프로젝트에 접근할 수 있습니다 https //github com/templates back4app/ios install sdk 전제 조건 이 튜토리얼에서는 xcode 9 1과 ios 11 로 생성된 기본 앱을 사용할 것입니다 이 튜토리얼을 완료하려면 다음이 필요합니다 back4app에서 생성된 앱 참고 https //www back4app com/docs/get started/new parse app 을 따라 back4app에서 앱을 만드는 방법을 배우세요 xcode 기본 ios 앱 참고 기본 앱이 없다면 xcode를 열고 파일 > 새로 만들기 > 프로젝트 > ios 그런 다음 앱 을 선택하세요 기본 앱을 만든 후 이 가이드를 따를 준비가 됩니다 참고 parse ios sdk는 ios 7 0 이상에서 작동합니다 1 템플릿 가져오기 템플릿을 다운로드하려면 https //github com/back4app/ios objective c quickstart example/archive/master zip , 그리고 프로젝트 폴더에서 파일을 압축 해제하세요 다음 명령어를 사용하여 수행할 수 있습니다 $ curl lok https //github com/back4app/ios objective c quickstart example/archive/master zip && unzip master zip 2 프로젝트 템플릿 열기 xcode를 엽니다 클릭하세요 파일 >열기 파일 >열기 3\ 프로젝트 폴더로 이동하여 quickstartobjcexampleapp xcworkspace quickstartobjcexampleapp xcworkspace 을(를) 두 번 클릭합니다 4\ xcode가 프로젝트를 열 때까지 기다립니다 3 차이 이해하기 일반적으로 objective c에서는 쿼리를 작성하는 두 가지 옵션이 있습니다 ‘pfquery’를 사용하거나 ‘nspredicate’를 사용하는 것입니다 두 가지 모두 비슷하게 작동하지만, 사용하려는 제약 조건의 수에 따라 하나를 사용하는 것이 더 합리적일 수 있습니다 예를 들어, pfquery를 사용한 간단한 쿼리는 다음과 같습니다 1 \[query wherekey @"playername" notequalto @"michael yabuti"]; 2 \[query wherekey @"playerage" greaterthan @18]; 하지만 더 복잡한 쿼리는 다음과 같이 될 수 있습니다 1 \[query wherekey @"playername" notequalto @"michael yabuti"]; 2 \[query wherekey @"playerage" greaterthan @18]; 3 \[query wherekey @"playerheight" greaterthan @180]; 4 \[query wherekey @"playerweight" greaterthan @80]; 5 \[query wherekey @"playerfavoritecolour" notequalto @"blue"]; 6 \[query wherekey @"playerislefthanded" equalto @true]; 7 \[query wherekey @"playershoesize" notequalto @42]; 8 \[query wherekey @"playerlivingstate" equalto @"arizona"]; 9 \[query wherekey @"playerlivingcity" notequalto @"springfield"]; 10 \[query wherekey @"playermothersname" equalto @"jane"]; 따라서 각 경우에 따라 ‘nspredicate’를 대신 사용할 수 있습니다 ‘nspredicate’를 사용한 간단한 쿼리는 다음과 같습니다 1 nspredicate predicate = \[nspredicate predicatewithformat @"playername != 'michael yabuti' and playerage > 18"]; 2 pfquery query = \[pfquery querywithclassname @"gamescore" predicate\ predicate]; 더 복잡한 쿼리는 다음과 같이 될 수 있습니다 1 nspredicate predicate = \[nspredicate predicatewithformat @"playername != 'michael yabuti' and playerage > 18 and playerheight > 180 and playerweight > 80 and playerfavoritecolour != 'blue' and playerislefthanded = true and playershoesize != 42 and playerlivingstate = 'arizona' and playerlivingcity != 'springfield' and playermothersname = 'jane'"]; 2 pfquery query = \[pfquery querywithclassname @"gamescore" predicate\ predicate]; 4 쿼리 실행하기 그럼, 쿼리를 실행할 수 있습니다 1 \[query findobjectsinbackgroundwithblock ^(nsarray objects, nserror error) { 2 if (!error) { 3 // 요청 성공 4 } 5 }]; 다음 단계 이 시점에서, ios 앱을 시작하는 방법을 배웠습니다 우리의 https //www back4app com/docs/ios/ios app template 을 둘러보며 더 알아보거나 https //docs parseplatform org/ios/guide/ 를 확인하세요