1

I have a library function written in Objective C that has takes a pointer to a NSMutableArray. However, when I try to call it with a Swift array I get this error:

DiscoverViewController.swift:34:20: Could not find an overload for 'init' that
accepts the supplied arguments

However, if I pass nil, it works. That makes sense.

Here is an example of the Objective-C:

int ex:(NSMutableArray *)in { return in.count; }

And an example of the Swift:

ex([1,2,3,4]) // doesn't compile

ex(nil) // does compile

What do I have to do to convert/cast the Swift array to the NSMutableArray type that my library function is looking for?

Here is the full method signature in case my example is too simplified:

- (id) initWithTitle: (NSString *) title place: (PublicPlaceInfo *) place
datetimeInterval: (DateTimeInterval *) datetimeInterval costBracket: (int)
costBracket creatorId: (PersonID) creatorId cover: (Url) cover experienceId: 
(ExperienceID) experienceId numJoined: (int32_t) numJoined relatedJoined: 
(NSMutableArray *) relatedJoined;

And then I know it is failing at the relatedJoined section because I converted all other potentially anonymous instantiations to variables. I have to say, this is certainly not the most helpful compile error..

0

1 Answer 1

6
ex([1,2,3,4]) // doesn't compile

This is because you're passing it a normal array, not a mutable array. Try converting it to a mutable array first: ex(NSMutableArray([1,2,3,4]))

ex(nil) // does compile

nil is a valid argument to NSMutableArray? which is why it works.

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

3 Comments

Ok so then I let the relatedJoined parameter equal to a var mutArr = [1,2,3,4] and it still does not compile. But according to the documentation, an array that is declared as "var" (not "let") is a mutable array.
@phileaton: Swift's Array is not the same as NSArray. Docs reference Think of an Array in Swift to be a SWArray :-)
Haha alright, I guess I'm just disappointed there isn't native support since for conversion since NSArray and NSMutableArray are so fundamental in Objective-C.

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.