Title: Declaring a multi-type array
Tags: ios;arrays;swift
Question: I would like to have my function return an array in which the first element is a String and the second is a UIImageView object. E.g.
["An example string", UIImageView()]
How do I tell the function that this will be returned in the section that follows the ->
So basically I want a function like this:
func DoSomething(num:Int) -&gt; Array[String, UIImageView()] {
// Each of the following objects in the Array are UIImageView objects
Let imageViewObjects = [image1, image2, image3]
return [String(num), imageViewObjects[num]]
But the part I know I am getting wrong is the
Array[String, UIImageView]
P.S. I need to declare this because if I use [AnyObject] it will raise an error later on in the code basically saying that it cant manipulate an object of type AnyObject
Comment: Do you want to return a single `UIImageView` or an array of them? Also, num is an `Int` but you are returning it as a `String`. Which do you want?

Comment: Are you only returning a single set of `String` and `UIImageView`? Why return an `Array` if you're only returning one set of elements?

Here is the accepted answer: Note that an Array is declared as [Int] or Array<Int&gt; rather than Array[Int]. [Int] and Array<Int&gt; are the same thing. You don't need to use gabriel19@example.net.
The easiest thing to do is use a tuple, declared like this:
(String, UIImageView)
I would use it like this (your code with corrections):
import UIKit
typealias StringView = (String, UIImageView)
// Returning an Optional to pass back that num may be out of range
// note that I'm using the typealias StringView here
func DoSomething(num:Int) -&gt; StringView? {
let image1 = UIImageView()
let image2 = UIImageView()
let image3 = UIImageView()
let imageViewObjects = [image1, image2, image3]
// Need to check that num is a valid index
guard num < imageViewObjects.count else { return nil }
// return the tuple if num is valid
return (String(num), imageViewObjects[num])
Example usage:
if let returned = DoSomething(2) {
// printing the first item in returned tuple
print(returned.0)
// output: "2"
You can also use protocols to create a common protocol, add it to the classes in an extension, and then use the protocol in the declaration:
protocol Foo {}
extension String : Foo {}
extension UIImageView: Foo {}
var myArray:[Foo] ...
If you're going to use the returned value in a lot of places you might want to make it a full-blown struct or class:
import UIKit
struct StringView {
let string:String
let view:UIImageView
// Returning an Optional to pass back that num may be out of range
// note that I'm using the typealias StringView here
func DoSomething(num:Int) -&gt; StringView? {
let imageViewObjects = [UIImageView(),
UIImageView(),
UIImageView()]
// Need to check that num is a valid index
guard num < imageViewObjects.count else { return nil }
// return the tuple if num is valid
return StringView(string: String(num), view: imageViewObjects[num])
if let returned = DoSomething(2) {
// printing the member "string" in the returned struct
print(returned.string)
// output: "2"
A tuple is usually the better choice unless you are using custom structs and classes.
Comment for this answer: You can name each element in the tuple like: `(string:String, view:View)` and access it like: `my tuple.string` or just use an index: `mytuple.0` which would be the first element, the `String`. A `typealias` might be nice too: `typealias StringView = (String, UIView)` - then you can use `StringView` wherever you need to declare the tuple.
Comment for this answer: @Iso1234 I edited the example to return a single `tuple` rather than an `Array` of them.

Here is another answer: You can use Dictionary objects in swift, here you can use key as your String and value as array of ImageView
let imageViewObjects = [image1, image2, image3]
let array : [String : UIImageView] = [
String(num) : imageViewObjects[num]
If you want to return only array you can do like this
let imageViewObjects = [image1, image2, image3]
var array : [AnyObject] = [AnyObject]()
array.append(String(num))
array.append(imageViewObjects[num])
In this you have to be sure that first object is a String and second is array of UIImageView

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:06/17/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ios><arrays><swift>', 'Title': 'Declaring a multi-type array', 'ViewCount': 79, 'id': 37880044, 'user_AboutMe': '<p>"Mathematics, rightly viewed, possesses not only truth, but supreme beauty a beauty cold and austere, like that of sculpture, without appeal to any part of our weaker nature, without the georgeous trappings of painting or music, yet sublimely pure, and capable of a stern perfection such as only the greatest art can show. The true spirit of delight, the exaltation, the sense of being more than Man, which is the touchstone of the highest excellence, is to be found in mathematics as surely as in poetry." - Bertrand Russell</p>\n', 'user_AccountId': 8420624.0, 'user_DisplayName': 'Isaac Greene', 'user_Reputation': 161.0, 'user_Views': 15.0, 'user_id': 6319543.0}}