在Swift中将值存储在Array Int中 [英] Store value in Array Int in Swift

查看:56
本文介绍了在Swift中将值存储在Array Int中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将结果打印结果存储在整数数组中.我尝试将值附加到空数组中,但在打印中显示为空.下面是我完成之前的快速代码.

How to store the resultant print in an Array of Integer. I try to append the value in empty array but in the print it shows me empty. Below is the swift code till what i have done.

var num = [Int]()
            var filterMenId = ""
            if let indexPath = self.colEat.indexPathsForSelectedItems?.first{
                filterMenId = self.arrayMeal[indexPath.row].id.description
                let convert = Int(filterMenId) // Here i am getting the value, eg: 32
                print(num.append(convert!)) // Here i try to append but shows me empty

推荐答案

append 通过在数组 only .它不会产生值.

print 接受一个 value 并将其打印出来.好吧, append 不会产生值,所以 print(num.append(convert!))打印(),它表示无"

print takes a value and prints it. Well, append does not produce a value, so print(num.append(convert!)) prints (), which is a representation of "nothing"

从技术上讲, append 返回 Void ,这是空元组()的类型别名.毫无疑问,空元组的字符串表示形式是().

More technically speaking, append returns Void, which is a typealias for an empty tuple (). The string representation of an empty tuple is, unsurprisingly, ().

如果要打印将 convert 添加到 num 时将产生的数组:

If you want to print the array that would be produced if convert is added to num:

print(num + [convert!])

请注意,这不会更改 num .

如果要在 num 中添加 convert ,然后打印 num 以显示更改后的元素,则需要两个行:

If you want to add convert to num, and then print num to show what elements it has after the change, you need two lines:

num.append(convert!)
print(num)

这篇关于在Swift中将值存储在Array Int中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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