Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Swift array of [Int64] into NSArray

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

like image 848
UrK Avatar asked Mar 24 '15 13:03

UrK


1 Answers

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.

like image 122
Nikita Kukushkin Avatar answered Nov 07 '22 18:11

Nikita Kukushkin