Published on: May 16, 2020

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

  1. The code has "missing return" syntax error
  2. The code has syntax error but not "missing return"
  3. 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

  1. The Line 1 has syntax error
  2. The Line 2 has syntax error
  3. The Line 3 has syntax error
  4. The method anyTest has syntax error
  5. eraseToAnyPublisher in method anyTest 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

  1. The method someTest has syntax error
  2. Just can be a return type some Publisher
  3. The Line 5 has syntax error
  4. The Line 6 has syntax error
  5. The Line 7 has syntax error
  6. We can call anyTest of Q2 and someTest 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

  1. The method genericTest has syntax error
  2. Just(9) as! T the cast is not required
  3. where T.Output == Int, T.Failure == Never is incorrect syntax.
  4. The Line 8 has syntax error
  5. The Line 10 has syntax error
  6. The Line 10 is correct but line 11 has syntax error
  7. We can call someTest of Q3 and genericTest interchangeably.

Check your Score

Answer 1

Hints



Answer 2

Hints



Answer 3

Hints



Answer 4

Hints

  • Compiler cannot infer the Generic parameter 'T' unless you explicitly declare type of p4.
  • Read about Generics

Reward yourself 25 points for each correct answer.

Don't be shy and share your score

Tagged with: