Swift 2 array of tuples not working as in swift 1
You're appending the parameters list, not the tuple itself: you're missing a pair of ()
.
rows.append((title: "Foo", body:"Bar", icon: "Bas", iconColor: UIColor(netHex: 0x4285f4)))
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 would I join two arrays using zip but not returning tuples?
Without using Alladinian intermediate map, since it's useless:
let a = [1, 2, 3, 4, 5]
let b = [100, 200, 400, 800, 1600]
let c = zip(a, b).flatMap { [$0.0, $0.1] }
merging two arrays of tuples
You can use a single map
to achieve your calls. You call map
on weightCalculated
, which you know for sure will contain an entry for each day, then check if weightsMeasured
contains an entry for the same day or not. If there is a measurement, return the measurement, otherwise return the calculated value.
let mergedWeights = weightsCalculated.map({ todayCalculated->PointTuple in
if let todayMeasured = weightsMeasured.first(where: { $0.day == todayCalculated.day}) {
return todayMeasured
} else {
return todayCalculated
}
})
Or you can even write this as a one liner using the nil-coalescing operator to return the measured value if it is found and the calculated value otherwise:
let mergedWeights = weightsCalculated.map({ todayCalculated in return weightsMeasured.first(where: { $0.day == todayCalculated.day}) ?? todayCalculated })
When trying out the above code bear in mind that you had a typo in the variable name weightCalculated
, which I corrected in my code.
Tuple containing an array (and assigned as variable) not working?
This should work:
let arrayOfObjects : [myObject] = []
tuples.append(letter:"t", objects: arrayOfObjects)
As you can see, arrayOfObjects
is a constant. The problem here might stem from the fact that append
expects a constant parameter T
, while you are passing a tuple containing a var
.
IMHO this is what makes append
goes a little crazy, and the compiler gives a crazier error description ;-)
Problems mixing Arrays, Tuples and Closures in Swift
2 and 3's ambiguity seems to be due to Swift compiler bugs, as is using menuItems[0].closure
directly which causes compiler segmentation faults.
1 works fine once I remove the parentheses for the tuple, which also seems non-logical.
menuItems.append(title: "any", closure: {})
Anyway I expect this to change/break in future Swift/compiler updates.
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]()
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
}
}
Related Topics
Listing All Files in a Folder Recursively with Swift
Swiftui List Empty State View/Modifier
Swift 4.2 Imagepickercontroller Issue
How to Find Actual Swiftui API Documentation (And Not Just the Developer Documentation)
Swift 3: Convert a Null-Terminated Unsafepointer<Uint8> to a String
Two Tables on One View in Swift
Using Stringbyreplacingcharactersinrange in Swift
Contextual Type for Closure Argument List Expects 1 Argument, But 4 Were Specified
Does Swift Have a Null Coalescing Operator and If Not, What Is an Example of a Custom Operator
iOS Charts 3.0 - Align X Labels (Dates) with Plots
When and How to Use @Noreturn Attribute in Swift
Reading Currently Playing Track in MACos Using Scriptingbridge Not Working
Background Request Not Execute Alamofire Swift
In Swift, What Does It Mean for Protocol to Inherit from Class Keyword
How Do People Deal with Iterating a Swift Struct Value-Type Property