Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Tuples as AnyObject in swift

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?

like image 803
Salman Hasrat Khan Avatar asked Jul 26 '14 11:07

Salman Hasrat Khan


People also ask

What is tuple in Swift?

A tuple type is a comma-separated list of types, enclosed in parentheses. You can use a tuple type as the return type of a function to enable the function to return a single tuple containing multiple values.


2 Answers

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)
like image 96
freytag Avatar answered Sep 28 '22 04:09

freytag


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]
like image 39
codester Avatar answered Sep 28 '22 05:09

codester