Appending tuples to an array of tuples

风格不统一 提交于 2019-12-18 07:14:05

问题


My class declares an array

var laps: (start: NSDate!, end: NSDate!)[] = []

When a tuple is added to this array I'd like to be able to do something like

let now = NSDate()
var lap = (now, nil)
laps.append(lap)

But at the append I get the error Missing argument for parameter 'end' in call.


回答1:


I've tried to following, and it looked correct syntactically:

typealias MyTuple = (start: NSDate!, end: NSDate?)

then in the method, I did:

var laps: Array<MyTuple> = Array()
laps.append((NSDate.date(), nil))



回答2:


There's a bug in using .append with arrays of tuples. You can use the += operator instead:

laps += lap


来源:https://stackoverflow.com/questions/24575331/appending-tuples-to-an-array-of-tuples

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!