0Pricing
SwiftUI Academy · บทเรียน

เพรดิเคตและตัวบอกการเรียงลำดับ

กรองและเรียงลำดับผลลัพธ์ที่ดึงมา

เพรดิเคตและตัวบอกการเรียงลำดับ เป็นบทเรียน SwiftUI Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน SwiftUI Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Shaping Your Results

As data grows you rarely want every row. Predicates and sort descriptors let you fetch just the right slice.

What a Predicate Is

An NSPredicate is a rule that decides which records match, written in a compact query-like format string.

NSPredicate(format: "isDone == true")

Comparing Values

Use operators like ==, >, and < in the format string to match numbers, dates, and other fields.

NSPredicate(format: "priority > 2")

Safe Arguments

Insert dynamic values with %@ placeholders instead of string-building, which is safer and clearer.

NSPredicate(format: "title == %@", searchText)

Text Matching

Match partial text with CONTAINS, and add [c] to ignore case for friendly search.

NSPredicate(format: "title CONTAINS[c] %@", query)

Combining Conditions

Join rules with AND and OR to express more specific filters in one predicate.

NSPredicate(format: "isDone == false AND priority > 1")

What a Sort Descriptor Is

A SortDescriptor tells Core Data which attribute orders the rows, and in which direction.

SortDescriptor(\.title, order: .forward)

Multiple Sort Keys

Pass an array of descriptors to break ties, like sorting by priority first, then by name.

[SortDescriptor(\.priority), SortDescriptor(\.title)]

Using Them in @FetchRequest

Combine both in a @FetchRequest to fetch a filtered, ordered set right inside your view.

@FetchRequest(sortDescriptors: [SortDescriptor(\.title)], predicate: NSPredicate(format: "isDone == false")) var tasks: FetchedResults<Task>

Filtering Beats Looping

Letting the store filter and sort is faster than fetching everything and trimming it in Swift. ⚡

Update Filters Live

You can rebuild a predicate from a search field and reassign it, so results refine as the user types.

Quick Check

You can now filter and order data. Quick check on the tools.

Recap

You used predicates to filter records and sort descriptors to order them, fetching exactly the data each screen needs. ✨

คำถามที่พบบ่อย

บทเรียน “เพรดิเคตและตัวบอกการเรียงลำดับ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เพรดิเคตและตัวบอกการเรียงลำดับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส SwiftUI Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส SwiftUI Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เพรดิเคตและตัวบอกการเรียงลำดับ”

กรองและเรียงลำดับผลลัพธ์ที่ดึงมา คุณปฏิบัติ SwiftUI Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน SwiftUI Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน SwiftUI Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “เพรดิเคตและตัวบอกการเรียงลำดับ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน SwiftUI Academy นี้ได้ไหม

ได้ บทเรียน SwiftUI Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. สแตก Core Data
  2. ดึงข้อมูลด้วย @FetchRequest
  3. สร้างและบันทึกเอนทิตี
  4. เพรดิเคตและตัวบอกการเรียงลำดับ
← กลับไปที่ SwiftUI Academy