0
0
iOS Swiftmobile~10 mins

Predicates and sorting in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A'A'
B"A"
C"a"
DA
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.
2fill in blank
medium

Complete 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'
A<
B>
C==
D>=
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.
3fill in blank
hard

Fix 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'
A==
B=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of '==' or '>'.
Using '<' which filters ages less than 18.
Using '==' which filters only age exactly 18.
4fill in blank
hard

Fill 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'
A"John"
B<
C>
D"john"
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.
5fill in blank
hard

Fill 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'
A>
B<
C$0.name
D$1.name
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.