ios - 我如何定义元素并将其附加到字典数组
问题描述
我正在研究一个项目,它读取一个包含多行字典的 plist 和一个名为国家的数组包含国家,国家数组填充了我的 plist 的数据并且一切正常,现在我需要定义一个元素并使用它来附加到我的数组,
这是我的国家定义
struct Country : Codable {
let orFlagEmoji, destFlagEmoji, : String
private enum CointryKeys : String, CodingKey { case orFlagEmoji,destFlagEmoji }
}
var countries = [Country]()
override func viewDidLoad()
{
super.viewDidLoad()
let urlPlist = Bundle.main.url(forResource: "ListinFirstPage", withExtension: "plist")!
let data = try! Data(contentsOf: urlPlist)
do
{
countries = try PropertyListDecoder().decode([Country].self, from: data)
}
catch
{
// Handle error
print(error)
}
//The problem is in two line bottom
var test = [TableViewController.Country(orFlagEmoji: "something", destFlagEmoji: "one thing")]
countries.append(test)
}
当我添加这两行
var test = [TableViewController.Country(orFlagEmoji: "something", destFlagEmoji: "one thing")]
countries.append(test)
它遇到了这个错误
Cannot convert value of type '[TableViewController.Country]' to expected argument type 'TableViewController.Country'
,我非常感谢您的帮助。谢谢你
解决方案
只需删除周围的括号test
:
var test = TableViewController.Country(orFlagEmoji: "something", destFlagEmoji: "one thing")
countries.append(test)
或者,如果您需要连接两个数组,请执行以下操作:
var test = [TableViewController.Country(orFlagEmoji: "something", destFlagEmoji: "one thing")]
countries += test
推荐阅读
- android - ScrollView 根本无法滚动
- json - 使用 JQ 将 JSON 嵌套到 CSV
- c# - DataTable.Load() 将枚举转换为整数
- python - 如何按单个单词分组并计算包含元组项的列表的平均分数?
- python - Pubsub 模拟器重新发送确认的消息
- python - Python:如何使用 IMAP 从收件箱中的某些电子邮件地址中提取姓名?
- reporting - 使用 Microstrategy 生成 Erwin 9+
- javascript - 使用 JavaScript 的 Webhook
- php - 未找到 Laravel 'Illuminate\Support\Facades\Storage'
- powershell - 在本地使用 PowerShell 获取分支上的 Azure DevOps 上次构建 ID