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?
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.
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.
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)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With