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

2
  • stackoverflow.com/questions/27305855/… Commented Mar 24, 2015 at 13:12
  • @DheerajSingh, the question you posted relates to a single element of an array. I am trying to cast the array itself. Commented Mar 24, 2015 at 13:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.