Search code examples
iosswiftsyntaxnlp-question-answering

Interview question Swift, incomprehensible syntax


In the WWW I find interview question and one of them is:

What will be the output of the code snippet below:

class Kondana<T:Equatable> {
    var dictDataHolder = [String:T]()
    func add(value:T?,using key:String) -> T? {
         self.dictDataHolder[key] = value
         return value
    }
}
var fortOne = Kondana<String>()
let value = fortOne.
print(value)

Answers are:

a) British
b) nil
c) compile-time error
d) segmentation fault

I am run this code in Xcode and my output is:

__lldb_expr_21.Kondana<Swift.String>

Help me please understand what is going on step by step. I have thoughts about what is going on but not sure about them. What is Kondana class and why use the syntax like above, I know that is generic but not understand the output?


Solution

  • With the code posted, the correct answer is "compile-time error". As others have said, the line

    let value = fortOne. 
    

    is truncated, and is therefore not legal.