Advanced Swift Quiz
There are some Swift concepts which often surprise or confuse even very experienced programmers. Let's find out how good you are in understanding some of the advanced Swift skills by answering the quiz below using Combine.
Before starting if you want, take some time to read about Publisher and Just which are used in all problems you are about to see.
Now open a notebook to write down your answers and check in the end how many points you got and do not cheat 🤓
Question 1
//Method 1
func patTtest() -> Publisher {
Just(9)
}
Write down all the correct statements
- The code has "missing return" syntax error
- The code has syntax error but not "missing return"
- The code has no syntax error
Question 2
//Method 2
func anyTest() -> AnyPublisher<Int, Never> {
Just(9).eraseToAnyPublisher()
}
let p2 = anyTest() //line 1
p2.sink { print($0) } //line 2
let p22: Just<Int> = anyTest() //line 3
p22.sink { print($0) } //line 4
Write down all the correct statements
- The Line 1 has syntax error
- The Line 2 has syntax error
- The Line 3 has syntax error
- The method
anyTest
has syntax error eraseToAnyPublisher
in methodanyTest
is not required.
Question 3
//Method 3
func someTest() -> some Publisher {
Just(9)
}
let p3 = someTest() //line 5
p3.sink { print($0) } //line 6
let p33: Just<Int> = someTest() //line 7
Write down all the correct statements
- The method
someTest
has syntax error - Just can be a return type
some Publisher
- The Line 5 has syntax error
- The Line 6 has syntax error
- The Line 7 has syntax error
- We can call
anyTest
of Q2 andsomeTest
interchangeably.
Question 4
//Method 4
func genericTest<T: Publisher>() -> T where T.Output == Int, T.Failure == Never {
Just(9) as! T
}
let p4 = genericTest() // line 8
p4.sink { print($0) } // line 9
let p44: Just<Int> = genericTest() // line 10
p44.sink { print($0) } // line 11
Write down all the correct statements
- The method
genericTest
has syntax error Just(9) as! T
the cast is not requiredwhere T.Output == Int, T.Failure == Never
is incorrect syntax.- The Line 8 has syntax error
- The Line 10 has syntax error
- The Line 10 is correct but line 11 has syntax error
- We can call
someTest
of Q3 andgenericTest
interchangeably.
Check your Score
Answer 1
Hints
- Publisher is a protocol with associatedtype (Output and Failure) so it cannot be used a a type.
- Read about protocols with Associated types
Answer 2
Hints
- The type of p22, 'Just<Int>' is not same as return type of method 'AnyPublisher<Int, Never>'
- Read about Publisher type
- Read about Any[Type] concept
Answer 3
Hints
- Check the type requirements for a subscriber to subscribe to a Publisher. The subscriber’s Input and Failure associated types must match the Output and Failure types declared by the publisher.
- 'some Publisher' is not same as 'Just
' - Check out various answers in this SO post
- Proposal about opaque return types
- Donny Wals explaining "some" concepts here
Answer 4
Hints
- Compiler cannot infer the Generic parameter 'T' unless you explicitly declare type of p4.
- Read about Generics