Ios 如何定义一个元素并将其附加到字典数组中
我正在做一个项目,它读取一个plist包含多行字典,一个名为countries包含country的数组,countries数组填充了我的plist的数据,所有工作正常,现在我需要定义一个元素并使用它附加到我的数组中 这是我的国家定义Ios 如何定义一个元素并将其附加到字典数组中,ios,arrays,swift,dictionary,plist,Ios,Arrays,Swift,Dictionary,Plist,我正在做一个项目,它读取一个plist包含多行字典,一个名为countries包含country的数组,countries数组填充了我的plist的数据,所有工作正常,现在我需要定义一个元素并使用它附加到我的数组中 这是我的国家定义 struct Country : Codable { let orFlagEmoji, destFlagEmoji, : String private enum CointryKeys : String, CodingKey { case orFl
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)
它面临着这一错误
无法将“[TableViewController.Country]”类型的值转换为预期的参数类型“TableViewController.Country”
,我真的很感谢你的帮助。谢谢只需删除
测试
周围的括号即可:
var test=TableViewController.Country(orFlagEmoji:“某物”,destFlagEmoji:“一物”)
国家。附加(测试)
或者,如果需要连接两个数组,请执行以下操作:
var test=[TableViewController.Country(orFlagEmoji:“某物”,destFlagEmoji:“一物”)]
国家+=测试
是的,非常感谢,很有效,非常感谢David