如何创建的元组数组? [英] How do I create an array of tuples?

查看:167
本文介绍了如何创建的元组数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造斯威夫特元组的数组,但有很大的难度:

  VAR乐趣:(NUM1:智力,NUM2:智力)[] =(NUM1:智力,NUM2:智力)[]()

以上会导致编译错误。

为什么这么错了吗?以下正常工作:

  VAR富:INT [] = INT []()


解决方案

它与一个类型别名:

  typealias mytuple =(NUM1:智力,NUM2:智力)VAR乐趣:mytuple [] = mytuple []()
//或者只是:VAR乐趣= mytuple []()
fun.append((1,2))
fun.append((3,4))的println(有趣)
// [(1,2),(3,4)]

更新:的作为X code 6 Beta 3中时,数组的语法发生了变化:

  VAR乐趣:[mytuple] = [mytuple]()
//或者只是:VAR乐趣= [mytuple]()

I'm trying to create an array of tuples in Swift, but having great difficulty:

var fun: (num1: Int, num2: Int)[] = (num1: Int, num2: Int)[]()

The above causes a compiler error.

Why's that wrong? The following works correctly:

var foo: Int[] = Int[]()

解决方案

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]()

这篇关于如何创建的元组数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆