ปรับคำค้นหา iOS ด้วย NSPredicate อย่างมีประสิทธิภาพ
10 นาที
การค้นหาด้วย nspredicate บทนำ ในส่วนนี้คุณจะได้เรียนรู้วิธีการใช้ nspredicate เพื่อกำหนดการค้นหาของคุณใน objective c ในทุกเวลา คุณสามารถเข้าถึงโปรเจกต์ทั้งหมดที่สร้างขึ้นด้วยบทเรียนนี้ได้ที่ ที่เก็บ github https //github com/templates back4app/ios install sdk ข้อกำหนดเบื้องต้น ในบทแนะนำนี้เราจะใช้แอปพื้นฐานที่สร้างขึ้นใน objective c ด้วย xcode 9 1 และ ios 11 ในการทำให้บทแนะนำนี้เสร็จสมบูรณ์ คุณต้องมี แอปที่สร้างขึ้นที่ back4app หมายเหตุ ติดตาม บทแนะนำการสร้างแอปใหม่ https //www back4app com/docs/get started/new parse app เพื่อเรียนรู้วิธีการสร้างแอปที่ back4app xcode แอป ios พื้นฐาน หมายเหตุ หากคุณไม่มีแอปพื้นฐานที่สร้างขึ้น คุณสามารถเปิด xcode และกด file > new > project > ios จากนั้นเลือก app หลังจากที่คุณสร้างแอปพื้นฐานของคุณแล้ว คุณก็พร้อมที่จะติดตามคู่มือนี้ หมายเหตุ parse ios sdk ทำงานกับ ios 7 0 หรือสูงกว่า 1 รับเทมเพลต ดาวน์โหลดเทมเพลตที่ ที่เก็บ github ของ back4app 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 คลิกที่ \<font color="#2166ae">ไฟล์ \>เปิด\</font> 3\ ไปที่โฟลเดอร์โปรเจกต์และดับเบิลคลิกที่ \<font color="#2166ae">quickstartobjcexampleapp xcworkspace\</font> 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 แล้ว เรียนรู้เพิ่มเติมโดยการเดินไปรอบๆ ios tutorials https //www back4app com/docs/ios/ios app template หรือเช็ค เอกสารเปิดแหล่งข้อมูล parse สำหรับ ios sdk https //docs parseplatform org/ios/guide/