在Xcode 8.2中编译大数组文字 [英] Compiling large array literal in Xcode 8.2

查看:54
本文介绍了在Xcode 8.2中编译大数组文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Swift存储大型多维数组的最佳方法是什么?

What is the best way to store a large multidimensional array with Swift?

我有一个4D整数数组,现在它在Xcode 8.2中的编译速度似乎减慢了,因为它约有200组9个整数(共1800个)。第一个数组由12个数组组成,然后每个数组都有8个数组,然后每个数组都有2个或更多数组,每个数组由9个整数组成。问题是我计划将数据量增加5到6倍。

I have a 4D array of integers that seems to have slowed down compiling in Xcode 8.2 now that its about 200 sets of 9 integers (1800 total). The first array is made up of 12 arrays, which then each has 8 arrays, which then each has 2 or more arrays, which are each made up of 9 integers. The thing is I plan to probably increase the amount to data by 5 or 6 times.

昨晚我无法编译我的应用程序,今天早上它终于可以工作了,但是它还是很慢。我认为问题会随着加到数组上而变得更加严重。

I couldn't compile my app last night and it finally worked this morning but it was still slow. I figure the problem will just get worse as I add to the array.

有人说只是将每个数组附加到viewDidLoad中,有人说要为数组加载数组txt文件,但我不知道该怎么做。

Some people say to just append each array in the viewDidLoad and some have mentioned loading the array for a txt file but I don't know how to do that.

推荐答案

很抱歉,我一直在开这个玩笑,但是您需要以与超人进入裤子相同的方式进行此操作:一次一只腿。从整体结构开始。然后继续制作最里面的数组,并将它们附加到外面的数组中,以建立最外面的数组。这将帮助您入门:

I'm sorry to keep making the same joke, but you need to do this the same way Superman gets into his pants: one leg at a time. Start with the overall structure. Then keep making the innermost arrays and appending them into the outer arrays to build up the outermost array. This will get you started:

var level1 : [[[[Int]]]] = []
var level2 : [[[Int]]] = []
var level3 : [[Int]] = []
// -----
level3 = []
let innermost1 = [1,2,3,4,5,6,7,8,9]
let innermost2 = [11,12,13,14,15,16,17,18,19]
level3.append(innermost1)
level3.append(innermost2)
level2.append(level3)
level1.append(level2)
// ... keep going ...

通过这种方式,我们不需要任何大/深数组字面量,并且项目可以编译

In this way, we never need any big / deep array literals, and the project will compile easily.

也请注意,每个级别的数组类型都是明确声明的。这样一来,Swift便比推论起来要快乐得多。

Note too that the type of the array at each level is explicitly declared. This makes Swift a lot happier than having to infer it.

这篇关于在Xcode 8.2中编译大数组文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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