Home » Swift » Pass Tuples as AnyObject in swift

Pass Tuples as AnyObject in swift

Posted by: admin November 30, 2017 Leave a comment


Is it possible to pass Tuples as parameters to functions that take AnyObject as the parameter?

I’m using the OneDrive SDK that allows you to pass a userState Parameter which is declared as AnyObject. The function is declared as follows:

func getWithPath(path:String, userState: AnyObject)

i’d like to call this method passing in a Tuple since I want to pass multiple values with different types like so:

getWithPath("me/skydrive", (name: "temp", callingDate: Date(), randomValue: 2345))

is it possible to pass a Tuple as a parameter where an AnyObject is expected?


Tuples are no Objects, so this won’t work. However if you change AnyObject to Any, you can pass objects as well as tuples:

func getWithPath(path:String, userState: Any)


From swift docs

You’ll have access to anything within a class or protocol that’s
marked with the @objc attribute as long as it’s compatible with
Objective-C. This excludes Swift-only features such as those listed


Enumerations defined in Swift

Structures defined in Swift

Top-level functions defined in Swift

Global variables defined in Swift

Typealiases defined in Swift

Swift-style variadics

Nested types

Curried functions

For example, a method that takes a generic type as an argument or
returns a tuple will not be usable from Objective-C.

So you can not use tuple for AnyObject as there is no matched objective c object for tuple.Instead use Dictionary to pass as parameter

Use dictionary instead

var abc:[String:AnyObject] = ["abc":123,"pqr":"not"] you can use AnyObject for thar

Use this for your dictionary

let userState:[String:AnyObject] = ["name": "temp", "callingDate": NSDate(), "randomValue": 2345]