0
0
iOS Swiftmobile~5 mins

Predicates and sorting in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an NSPredicate in Swift?
An NSPredicate is a way to filter collections or Core Data fetch requests by defining conditions that items must meet. Think of it like a filter that only lets through items matching certain rules.
Click to reveal answer
beginner
How do you create a predicate to find names starting with "A"?
You create it like this: NSPredicate(format: "name BEGINSWITH[c] %@", "A"). This means it looks for names starting with 'A', ignoring case.
Click to reveal answer
beginner
What is NSSortDescriptor used for?
NSSortDescriptor tells Swift how to sort a list of items. For example, you can sort by a property like 'age' in ascending or descending order.
Click to reveal answer
intermediate
How do you sort an array of people by age descending using NSSortDescriptor?
You create a sort descriptor like this: NSSortDescriptor(key: "age", ascending: false) and use it to sort the array.
Click to reveal answer
intermediate
Why use predicates and sort descriptors together?
Using predicates filters your data to only what you want, and sort descriptors organize that filtered data in the order you want. Together, they help you get exactly the right data, nicely ordered.
Click to reveal answer
What does NSPredicate do in Swift?
AFilters data based on conditions
BSorts data in ascending order
CCreates user interface elements
DManages memory allocation
Which of these is the correct way to create a predicate to find items where 'age' is greater than 18?
ANSPredicate(format: "age > 18")
BNSPredicate(format: "age >= 18")
CNSPredicate(format: "age < 18")
DNSPredicate(format: "age == 18")
What does NSSortDescriptor(key: "name", ascending: true) do?
AFilters items with key 'name'
BFilters items with name 'true'
CSorts items by name in descending order
DSorts items by name in ascending order
How can you combine multiple sort descriptors?
AChain them with '+' operator
BPut them in an array and apply together
CUse only one sort descriptor at a time
DUse NSPredicate instead
Which is true about predicates and sorting?
ABoth sort data
BPredicates order data; sorting filters data
CPredicates filter data; sorting orders data
DBoth filter data
Explain how you would filter a list of contacts to only those whose names start with 'J' and then sort them by last name ascending.
Think about how to write the predicate format and then create a sort descriptor.
You got /4 concepts.
    Describe the difference between NSPredicate and NSSortDescriptor and how they work together in data handling.
    Imagine filtering a list first, then arranging it.
    You got /3 concepts.