Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a predicate that filters names starting with "A".
iOS Swift
let predicate = NSPredicate(format: "name BEGINSWITH [1]")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes.
Not quoting the letter at all.
Using lowercase 'a' which won't match uppercase names.
✗ Incorrect
The predicate format requires the string "A" to be in double quotes to match names starting with uppercase A.
2fill in blank
mediumComplete the code to sort an array of strings in ascending order.
iOS Swift
let sortedNames = names.sorted(by: { $0 [1] $1 })
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' which sorts in descending order.
Using '==' which does not sort.
Using '>=' which is not a valid sorting comparator.
✗ Incorrect
Using '<' sorts the array in ascending order alphabetically.
3fill in blank
hardFix the error in the predicate format to filter ages greater than 18.
iOS Swift
let predicate = NSPredicate(format: "age [1] 18")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' or '>'.
Using '<' which filters ages less than 18.
Using '==' which filters only age exactly 18.
✗ Incorrect
The '>' operator correctly filters for ages greater than 18.
4fill in blank
hardFill both blanks to create a predicate filtering names containing "John" and sort them descending.
iOS Swift
let predicate = NSPredicate(format: "name CONTAINS[c] [1]") let sortedNames = names.sorted(by: { $0 [2] $1 })
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase 'john' which may not match case-insensitively without [c].
Using '<' which sorts ascending instead of descending.
Not quoting the string in the predicate.
✗ Incorrect
The predicate uses "John" with quotes and the sorting uses '>' for descending order.
5fill in blank
hardFill all three blanks to create a dictionary of names and ages filtered by age > 20 and sorted by name ascending.
iOS Swift
let filtered = people.filter { $0.age [1] 20 }
let sorted = filtered.sorted(by: { $0.name [2] $1.name })
let nameAgeDict = Dictionary(uniqueKeysWithValues: sorted.map { ([3], $0.age) }) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' in filter which filters ages less than 20.
Using '>' in sort which sorts descending.
Using '$1.name' as key in map which is invalid.
✗ Incorrect
Filter uses '>' for age > 20, sort uses '<' for ascending by name, and map uses '$0.name' as key.