1

We know that AnyObject can only represent an instance of any class type. Link

I have SimpleStruct structure:

struct SimpleStruct {
    var value: Int = 0
}

And casting() Function

func casting() { //1
    var ss1: AnyObject //2
   // ss1 = SimpleStruct(value: 10) //Value of type 'SimpleStruct' does not conform to 'AnyObject' in assignment
    ss1 = SimpleStruct(value: 10) as AnyObject //4
    if let ss = ss1 as? SimpleStruct { //Why is this casting succeed //5
        print(ss.value) //6  "print 10"
    }
}

In line 3 structure assignment in AnyObject var is not possible.

Why in line 4 SimpleStruct to AnyObject casting and assignment is successful?

Why in line 5 AnyObject to SimpleStruct casting and assignment is successful?

Saleh Enam Shohag
  • 1,039
  • 1
  • 12
  • 23

0 Answers0