Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift method to return a tuple in objective c class

Tags:

swift

I would like to return two objects from a method using swift.

How can this method be added to an existing objective c class to access the tuple from where ever the method is called, in non swift files?

Does the tuple need to be created in a swift class?

like image 755
some_id Avatar asked Jul 26 '14 17:07

some_id


People also ask

Is tuple an object in Swift?

In Swift, Tuple is a Compound Type that holds some properties together which are built up from Objects of Swift Named Types for example class, struct and enum.

How can I return multiple values from a function in Swift?

Swift's functions have a single return type, such as Int or String , but that doesn't mean we can only return a single value. In fact, there are two ways we can send back multiple pieces of data: Using a tuple, such as (name: String, age: Int) Using some sort of collection, such as an array or a dictionary.

What is tuple in Swift with example?

In Swift, a tuple is a group of different values. And, each value inside a tuple can be of different data types. Suppose we need to store information about the name and price of a product, we can create a tuple with a value to store name (string) and another value to store price (float)

Are Swift tuples ordered?

In Swift a tuple can consist of multiple different types. Tuples are ordered and elements of tuples can also be named. So we can access elements of a tuple both by position and name, and we can deconstruct data into a tuple using the assignment operator.


1 Answers

Tuple is not compatible in objective c.You can use Dictionary instead of tuple .From swift documentation.

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:

Generics
Tuple     //Tuple cannot be acesses from objective c
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.

You cannot use these types in objective c in which tuple is included and cannot be accessed and it is not bind with any type in objective c and not compatible with objective c.

However you can do each and every thing with Swift Dictionaries that you can do with tuple.Use Dictionary instead of tuple.

like image 74
codester Avatar answered Sep 19 '22 15:09

codester