قالب وردپرس درنا توس
Home / IOS Development / Swift Quiz in Review

Swift Quiz in Review

We recently tweeted our 20th Swift Quiz so we thought it would be a good time to take an account and review what we have learned so far. You can find the complete collection here.

The quiz is collected each week by Ahmet Yalcinkaya our " language design fuzzer ", who does a good job of exploring the Swift edges. We look for riddles that strike the right balance between obscurity and realism: They illustrate a problem that can happen in everyday use, but in a way that makes you think. And of course they have to fit in a tweet!

Twitter's compact expression may force an extra layer of obscurity, but this is not our goal. It also does not prioritize context, so it may be worth mentioning here: the puzzles are intended as quizzes not questions about blackboard interviews. They are a stimulus to think not a test of competence.

That said, let's take a look at your answers!


  2019 07 30 quiz review graph

This graph shows the percentage of correct votes for each poll. For the most part, success cases fluctuate around fifty percent. That is, in general, more than half of you are disposed to choose the right answer – the British can call this an "overwhelming majority".

There are some layouts, and it can be instructive to look at them more closely.

The publishers

Three quizzes left our voters despite a high turnout:

Quiz 3: Protocol Dispatch

  protocol drawing {
func render ()

extension Drawing {
func circle () {print ("protocol")}
func render () {circle ()}

class SVG: Drawing {
func circle () {print ("class")}

SVG (). Render ()

// What are the outputs?

/// Answer: protocol / class

This quiz had a success rate of 43 percent, with the highest number of votes.

When building an app, you might be wondering why a method is not called, even if you added a specific override (as we do above, with circle ). Unless the method is part of the protocol, it is not called, because only these methods will be sent dynamically. Note that you can add the method of Protocol and to a standard implementation in an extension. This still allows you to override the method in a matching type.

Quiz 20: Dictionary with Nil Values

  was dictWithNils: [String: Int?] = [
    "one": 1,
    "two": 2,
    "none": nil

dictWithNils ["two"] = null
dictWithNils ["none"] = null
la result = dictWithNils.count

// What is the result?

/// Answer: 1/3 / Compiler Error

This quiz had a 30 percent success rate, with the third highest number of votes.

We usually avoid dictionaries of optional values, because the behavior is confusing. The subscription used above has an optional value, and nil means: delete this key. However, for a casual reader this may not be obvious.

Quiz 21: Patterns that match variable values

  var x = 42

la result: Result  =. success (x)
x = 21

switch results {
case. success (x):
print (x)
case. error (_):
print ("Error")
print ("Default")

// What is the result?

/// answer: 42/21 / Error / default

This quiz had a 15 percent success rate, with the fourth highest vote.

It is quite difficult, because it is not at all obvious what is happening. The pattern match of .success (x) does not bind nor to the variable x but uses the value of x to match. In other words, it is true when result is equal to . Success (21) . In general, it is advisable to avoid variable shading: do not use a variable name from an external scope. Still, there are times when matching as above is exactly what you want. To be more aware of the intention, you can write it as follows: case. Success (la y) der y == x: . This is more common, but easier to spot when reading the code.

That's the part that counts!

Twenty-one questions later become very clear: choosing the right answer is much less important than thinking through the solutions.

Perhaps the best way to learn from quizzes like this is to reflect and make your best guess, and then try the puzzle on a playground. We tweet the solution a day after, and it can be instructive to compare our explanation with the range of analyzes offered in the responses. Learning how others approach a problem can illuminate unexpected difficulties and tell you a lot about how linguists expect a language to behave.

We don't always get the answers right; to us that's what makes a question interesting. Sometimes it's actually a "we didn't know this!" -The moment that gives rise to a question in the first place.

To join future quizzes, follow our Twitter . We try to publish every Wednesday around 4 pm in Berlin.

Enjoy! 1965

Source link