Swift 的元组中是否可以有 nil 值? [英] Is it possible to have a nil value in a tuple with Swift?

查看:48
本文介绍了Swift 的元组中是否可以有 nil 值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一些代码,以便在我正在开发的关于 Pokémon 的应用程序中将一些测试数据播种到 Core Data 数据库中.我的播种代码基于此:http://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/

I'm trying to write some code for seeding some test data into the Core Data database in an application I'm developing, about Pokémon. My code for seeding is based on this: http://www.andrewcbancroft.com/2015/02/25/using-swift-to-seed-a-core-data-database/

不过,我对一件事有一点小问题.我似乎无法在元组中放入 nil 值.

I am having a slight problem with one thing though. I don't seem to be able to put a nil value inside of a tuple.

我目前正在尝试将一些 Pokémon Moves 植入数据库.一个移动可以有一堆不同的属性,但是它的组合完全取决于移动本身.所有种子移动数据都在一个元组数组中.

I'm currently trying to seed some Pokémon Moves into the database. A move can have a bunch of different properties, but which combination it has it entirely dependent on the move itself. All seed Move data is in an array of tuples.

为了演示...

let moves = [
    (name: "Absorb", moveType: grass!, category: "Special", power: 20, accuracy: 100, powerpoints: 25, effect: "User recovers half the HP inflicted on opponent", speedPriority: 0),
    // Snip
]

……没问题.这是具有上述所有属性的移动,其中 speedPriority 为零意味着什么.但是,有些移动没有威力准确度属性,因为它们与特定移动无关.但是,在没有功能准确度命名元素的情况下在数组中创建第二个元组,例如...

...is fine. It's a move with all the above properties, where zero in speedPriority means something. However, some moves don't have a power or accuracy property, because they're irrelevant to that specific move. However, creating a second tuple in the array without the power or accuracy named elements, such as...

(name: "Acupressure", moveType: normal!, category: "Status", powerpoints: 30, effect: "Sharply raises a random stat", speedPriority: 0)

...可以理解地抛出错误

...understandably throws an error

元组类型 {firstTuple} 和 {secondTuple} 具有不同数量的元素(8 对 6)

Tuple types {firstTuple} and {secondTuple} have a different number of elements (8 vs. 6)

因为,元组具有不同数量的元素.所以相反,我尝试...

because, well, the tuples have different number of elements. So instead, I tried...

(name: "Acupressure", moveType: normal!, category: "Status", power: nil, accuracy: nil, powerpoints: 30, effect: "Sharply raises a random stat", speedPriority: 0)

但这也不起作用,因为它给出了错误:

but this also didn't work, as it gave the error:

类型Int"不符合协议NilLiteralConvertible"

Type 'Int' does not conform to protocol 'NilLiteralConvertible'

那么,有什么办法可以做我想做的事情吗?有没有办法在元组中放置一个 nil 值,或者以某种方式使其成为可选元素?感谢!

So, is there any way to do what I'm trying to do? Is there some way to either place a nil value inside the tuple, or somehow make it an optional element? With thanks!

推荐答案

您可以执行以下操作:

typealias PokemonMove = (name: String?, category: String?)

var move1 : PokemonMove = (name: nil, category: "Special")

let moves: [PokemonMove] = [
    (name: nil, category: "Special"),
    (name: "Absorb", category: "Special")
]

根据需要添加更多参数,我只取了两个参数来解释概念.

Add more parameters as you wish, I took just two parameters for explanation of concept.

这篇关于Swift 的元组中是否可以有 nil 值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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