-2

I have a UInt32 array of Ints defined as: var myArr : [UInt32] = [1, 2, 3] how can I convert it to AnyObject type ? I've tried the forced downcast as! AnyObject but the compiler gives this warning: treating a forced downcast to AnyObject as optional will never produce nil

JAHelia
  • 6,934
  • 17
  • 74
  • 134
  • Perhaps you should explain why you need to recast it. –  Feb 15 '16 at 05:32
  • it's a requirement in my project – JAHelia Feb 15 '16 at 05:39
  • i wonder why should this question be closed and why down voting it ? is there something wrong in the question ? – JAHelia Feb 15 '16 at 06:45
  • [It's an XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). You should have shown what you were originally trying to do in the question, instead of mentioning it after the fact. –  Feb 15 '16 at 07:10

1 Answers1

1

A UInt32 is not like an Int. You cannot cast a UInt32 to an AnyObject, as they are not bridged. You will have to wrap every UInt32 in the array in an NSNumber explicitly, using map.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • that would be very slow for a huge bytes array of [UInt32] – JAHelia Feb 15 '16 at 05:46
  • 1
    Maybe, but you are the one who wants to cast this thing, not me. And the same would be true of an array of Int except that the mapping is performed for you automatically. – matt Feb 15 '16 at 06:03
  • [Int] is bridged with AnyObject, i.e. if you assign [Int] to myDict["myArr"] where myDict is defined as myDict = [String: AnyObject]() then the compiler will not complain – JAHelia Feb 15 '16 at 06:44