简体   繁体   English

多种类型的Swift数组

[英]Swift Arrays of Multiple Types

I'm working to convert an iOS Obj-C project to Swift a class at a time. 我正在一次将iOS Obj-C项目转换为Swift类。 One Obj-C class involved the creation of an NSArray with even elements descriptive NSStrings and odd elements NSNumbers. 一个Obj-C类涉及一个NSArray的创建,该数组具有偶数个描述性NSStrings和奇数个NSNumbers。 Not a great piece of design in retrospect but perfectly valid and it did the job. 回想起来,这并不是一个伟大的设计,但是它完全有效,并且可以完成工作。 I've converted this to Swift: 我已经将其转换为Swift:

let coins = ["1p", 0.01 , "2p" , 0.02 , "5p" , 0.05 , "10p" , 0.10 , "20p" , 0.20 , "50p" , 0.50 , "99p" , 0.99 ,  "£1" , 1.00 ,  "£2" , 2.00 , "£5" , 5.00 , "£9.99" , 9.99 , "£10" , 10.00 , "£20" , 20.00]

I do realise that Swift enforces a single type in a one-dimensional array, so we wouldn't so much be talking an array of multiple types as an array of a flexible type - I expected it would infer AnyObject. 我确实意识到Swift在一个一维数组中强制执行一个单一类型,因此我们不会在讲多个类型的数组作为灵活类型的数组-我希望它会推断AnyObject。

I never got to find out however, because it seems this line is an Xcode killer. 但是,我从来没有发现过,因为这似乎是Xcode的杀手.。 In my overall project it causes indexing to hang, quickly becoming unresponsive and eventually consuming all system memory. 在我的整个项目中,它导致索引挂起,迅速变得无响应,并最终消耗所有系统内存。 Even this single line of code pasted into the init of a UIView subclass added to a blank single view application causes Xcode to hang on attempting to build in a similar fashion (i'm curious if others have the same experience). 即使将这行代码粘贴到添加到空白单视图应用程序的UIView子类的init中,也会导致Xcode尝试以类似的方式进行构建而挂起(我想知道其他人是否有相同的经验)。

Is there any legitimacy to what i'm attempting here, given that it is compilable code. 鉴于它是可编译的代码,我在这里尝试的内容是否合法? If so, have I stumbled upon a Swift bug, or perhaps failed to do something vital to allow this to work. 如果是这样,我是偶然发现了一个Swift错误,还是未能做一些重要的事情来使它正常工作。 I can obviously find a far better way to go about doing what I want, particularly in Swift, but I don't think arrays of multiple types are so rarely seen in Obj-C that this isn't worth understanding further. 我显然可以找到一种更好的方法来完成我想要的事情,尤其是在Swift中,但是我认为在Obj-C中很少见到多种类型的数组,因此不值得进一步了解。

Looks like you've run into a bug in Xcode/Swift with Array<AnyObject> and literals. 似乎您遇到了Array<AnyObject>和文字使用Xcode / Swift的错误。 I get the same result. 我得到相同的结果。 Seems to get exponentially worse the more elements I add (my RAM gets full after ~6 elements). 我添加的元素似乎呈指数级恶化(大约6个元素后我的RAM已满)。 As a workaround, it seems to work fine to create a mutable array (using var instead of let ) and adding the elements one by one. 作为一种解决方法,似乎可以创建一个可变数组(使用var而不是let )并逐个添加元素。 Janky without a doubt, but it doesn't crash Xcode. 毫无疑问,Janky不会使Xcode崩溃。

var coins: Array<AnyObject> = []
coins += "1p"
coins += 0.01
coins += "2p"
coins += 0.02
coins += "5p"
coins += 0.05
coins += "10p"
coins += 0.10
coins += "20p"
coins += 0.20
coins += "50p"
coins += 0.50
coins += "99p"
coins += 0.99
coins += "£1"
coins += 1.00
coins += "£2"
coins += 2.00
coins += "£5"
coins += 5.00
coins += "£9.99"
coins += 9.99
coins += "£10"
coins += 10.00
coins += "£20"
coins += 20.00

因为例如Int是一个结构实例而不是一个类实例,所以类型是Any []

var x : Any[] = [1,"2",3.0]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM