18

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?

Victorp
  • 13,636
  • 2
  • 51
  • 55
Salman Hasrat Khan
  • 1,971
  • 1
  • 20
  • 27
  • https://developer.apple.com/library/prerelease/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html – iPatel Jul 26 '14 at 11:55

2 Answers2

14

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)
freytag
  • 4,769
  • 2
  • 27
  • 32
11

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 here: 7 Generics

Tuples

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]
codester
  • 36,891
  • 10
  • 74
  • 72
  • The problem is that the I don't have consistent value types for the dictionary. I have a string, a bool, a int and many more as value types. How can I overcome this barrier? – Salman Hasrat Khan Jul 26 '14 at 12:07
  • As sdk is written in `Objective C` so you can not use `tuple`.You can send `Dictionary` as parameter and make key like `state` and set this key with NSString value or NSNumber value.When you are getting to use value you can check the `class` of `dictionary` value and do appropriate action. – codester Jul 26 '14 at 12:12
  • `tuple` is same as the `Dictionary` you can do everything with dictionary which you can do with `tuple` at toplevel – codester Jul 26 '14 at 12:14
  • okay so I'm trying to do this... let userState = ["name": "temp", "callingDate": Date(), "randomValue": 2345)] but swift won't allow me to do this because the value types are different i.e String, Date, Int. How can I get around this ? – Salman Hasrat Khan Jul 26 '14 at 12:15
  • Use `NSDate` instead of `Date` – codester Jul 26 '14 at 12:43
  • but I don't think Int works as AnyObject, so I need to use NSNumber instead? Phew seems to be a long workaround... – Salman Hasrat Khan Jul 26 '14 at 13:14
  • It is perfectly fine to use `Int` in dictionary and Swift `Dictionary` will automatically convert as `NSDictionary` in objective c and all `Int` or `Bool` will automatically convert as `NSNumber` in `objective c`.You do not have to do anything.Use my solution and pass the dictionary in answer as parameter – codester Jul 26 '14 at 16:12
  • Cool it seems to work. I also wanted to pass a closure to the function, is it possible for me to add a closure into the dictionary? – Salman Hasrat Khan Jul 26 '14 at 16:43
  • nice suggestion of using Dictionary instead of tuple where not supported. – Rohan Sanap May 18 '16 at 11:00