8

I am trying to convert array of Swift Int64 into NSArray with NSNumber values.

@interface A : NSObject
- (void)bar:(NSArray *)tips;
@end

Swift class inherits this Objective-C class:

class B : A {
    func foo(tips : [Int64]) {
        self.bar(tips)
    }
}

Swift code does not compile with the following error:

Type '[Int64]' does not conform to protocol 'AnyObject'

How can I convert [Int64] into NSArray with NSNumber instances?

P.S. I tried number of things and could not find a simple way to do this:

self.bar(NSArray(array: tips))
self.bar(tips as NSArray)

EDIT: this question relates does not relate to trying to build new NSArray from separate Int64 objects, but to convert existing array [Int64] into NSArray

Dennis
  • 2,119
  • 20
  • 29
UrK
  • 2,191
  • 2
  • 26
  • 42
  • http://stackoverflow.com/questions/27305855/swift-cast-int64-to-anyobject-for-nsmutablearray – Dheeraj Singh Mar 24 '15 at 13:12
  • @DheerajSingh, the question you posted relates to a single element of an array. I am trying to cast the array itself. – UrK Mar 24 '15 at 13:14

1 Answers1

9

Map over it:

func foo(tips : [Int64]) {
    bar(tips.map { NSNumber(longLong: $0) })
}

This will build a new array, wrapping all Int64 in NSNumber, this new array should be castable to NSArray with no problems.

Nikita Kukushkin
  • 14,648
  • 4
  • 37
  • 45