Swift Array to Array of Tuples

Swift array to array of tuples

Use zip and map:

let xaxis = ["monday", "tuesday", "wednesday", "thursday", "friday"]
let yaxis = [1, 2, 3, 4, 5]

let tuples = zip(xaxis, yaxis).map { ($0, $1) }

How do I add a tuple to a Swift Array?

Since this is still the top answer on google for adding tuples to an array, its worth noting that things have changed slightly in the latest release. namely:

when declaring/instantiating arrays; the type is now nested within the braces:

var stuff:[(name: String, value: Int)] = []

the compound assignment operator, +=, is now used for concatenating arrays; if adding a single item, it needs to be nested in an array:

stuff += [(name: "test 1", value: 1)]

it also worth noting that when using append() on an array containing named tuples, you can provide each property of the tuple you're adding as an argument to append():

stuff.append((name: "test 2", value: 2))

Swift convert an array to array of tuples

  • Step 1: Create a grouped dictionary

    let array = ["Shivam", "Shiv", "Shantanu", "Mayank", "Neeraj"]
    let dictionary = Dictionary(grouping: array, by: {String($0.prefix(1))})
  • Step 2: Actually there is no step 2 because you are discouraged from using tuples for persistent data storage, but if you really want a tuple array

    let tupleArray = dictionary.map { ($0.0, $0.1) }

A better data model than a tuple is a custom struct for example

struct Section {
let prefix : String
let items : [String]
}

then map the dictionary to the struct and sort the array by prefix

let sections = dictionary.map { Section(prefix: $0.0, items: $0.1) }.sorted{$0.prefix < $1.prefix}
print(sections)

Swift: Get an array of element from an array of tuples

That's simple:

answers.map { $0.number }

How can I convert an array to a tuple?

You can't do this because the size of a tuple is part of its type, and this isn't true for an array. If you know the array's length, you can just do let myTuple = (myArray[0], myArray[1], ...). Or you can build your architecture around tuples from the beginning. What are you trying to do?

This is a common problem which occurs in lots of other languages. See How do I convert a list to a tuple in Haskell?

Accessing an array of tuples Swift 4

You should do something like this:

class eventCell: UICollectionViewCell {
@IBOutlet private weak var eventTitle: UILabel!
@IBOutlet private weak var descriptionLabel:UILabel!
@IBOutlet private weak var eventImage: UIImageView!

typealias Event = (title:String, location:String, lat:CLLocationDegrees, long:CLLocationDegrees)

var eventArray = [Event]()

override func prepareForReuse() {
eventImage.image = nil
}

func lool() {
var event = Event(title: "a", location:"b", lat:5, long:4)
eventArray.append(event)
eventTitle.text = eventArray[0].title
}
}

How to remove a specific tuple from a array of tuples in swift

You can use RangeReplaceableCollection method removeAll(where:) and pass a predicate:

var arrayOfTuples = [(4, 4, "id1"), (3, 6, "id2"), (3, 6, "id3")]
arrayOfTuples.removeAll(where: {$2 == "id2"})
print(arrayOfTuples) // [(4, 4, "id1"), (3, 6, "id3")]

If you would like to remove only the first occurrence where the third element of your tuple is equal to "id2" you can use Collection's method firstIndex(where:):

if let index = arrayOfTuples.firstIndex(where: {$2 == "id2"}) {
arrayOfTuples.remove(at: index)
}

Map Tuples to Array Swift

You need to add both elements to an array in the closure and you also need to use flatMap instead of map to flatten out the nested array that map would produce.

let arrayOfTuples = [(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1)]
let flattenedArray = arrayOfTuples.flatMap{ [$0.0, $0.1] }

How do I create an array of tuples?

It works with a type alias:

typealias mytuple = (num1: Int, num2: Int)

var fun: mytuple[] = mytuple[]()
// Or just: var fun = mytuple[]()
fun.append((1,2))
fun.append((3,4))

println(fun)
// [(1, 2), (3, 4)]

Update: As of Xcode 6 Beta 3, the array syntax has changed:

var fun: [mytuple] = [mytuple]()
// Or just: var fun = [mytuple]()


Related Topics



Leave a reply



Submit