swift循环后如何获取所有数据数组?
How to get all data array after looping in swift?
我创建结构来添加数据,这是我的结构:
struct data {
var documentCode: String?
var attachmentSize: String?
var applicationType: String?
var fileExtension: String?
var file64: String
var contentType: String
}
然后我将一些字符串添加到结构中:
let allData = [
data(documentCode: "1", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6"),
data(documentCode: "a", attachmentSize: "b", applicationType: "c", fileExtension: "d", file64: "e", contentType: "f"),
data(documentCode: "12", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6")
]
这是我的循环,我 var datas = [String: String]()
将数据添加到字典:
override func viewWillAppear(_ animated: Bool) {
var datas = [String: String]()
let allData = [
data(documentCode: "1", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6"),
data(documentCode: "a", attachmentSize: "b", applicationType: "c", fileExtension: "d", file64: "e", contentType: "f"),
data(documentCode: "12", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6")
]
for checkData in allData{
datas = ["documentCode": checkData.documentCode, "applicationType": checkData.applicationType, "attachmentSize": checkData.attachmentSize, "fileExtension": checkData.fileExtension, "file64": checkData.file64, "contentType": checkData.contentType] as! [String : String]
print(datas)
}
print(datas)
}
当我在循环中打印数据时,我得到这样的数据:
["file64": "5", "applicationType": "3", "attachmentSize": "2", "fileExtension": "4", "contentType": "6", "documentCode": "1"]
["file64": "e", "fileExtension": "d", "documentCode": "a", "attachmentSize": "b", "contentType": "f", "applicationType": "c"]
["documentCode": "12", "fileExtension": "4", "attachmentSize": "2", "applicationType": "3", "file64": "5", "contentType": "6"]
但是当我在外部循环打印时,我只得到最后一个索引数组:
["file64": "5", "applicationType": "3", "attachmentSize": "2", "fileExtension": "4", "contentType": "6", "documentCode": "1"]
如何在循环外获取 allData
数组?
老实说,你似乎想要 allData
所以 print(allData)
.
为了解释您的示例,设置为 datas
的最后一项将是数组中的最后一个索引 ["file64": "5", "applicationType": "3", "attachmentSize": "2", "fileExtension": "4", "contentType": "6", "documentCode": "1"]
。
如果 datas
是一个位于循环内部的变量,print(datas)
的打印语句甚至不会工作。
我相信你只是想要print(allData)
。
在循环中,您使用相同的键更新了字典,结果字典中只有一个数据。要保存您需要使用字典数组的所有数据。
你可以像这样声明数据:
var datas : [[String: String]] = []
然后你可以在数组循环中追加:
for checkData in allData{
let data = ["documentCode": checkData.documentCode, "applicationType": checkData.applicationType, "attachmentSize": checkData.attachmentSize, "fileExtension": checkData.fileExtension, "file64": checkData.file64, "contentType": checkData.contentType] as! [String : String]
datas.append(data)
}
print(datas)
您刚刚创建了词典。你需要什么来创建“字典数组”。
更新代码:-
override func viewWillAppear(_ animated: Bool) {
var datas = [[String: String]]()
let allData = [
data(documentCode: "1", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6"),
data(documentCode: "a", attachmentSize: "b", applicationType: "c", fileExtension: "d", file64: "e", contentType: "f"),
data(documentCode: "12", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6")
]
for checkData in allData{
datas.append(["documentCode": checkData.documentCode, "applicationType": checkData.applicationType, "attachmentSize": checkData.attachmentSize, "fileExtension": checkData.fileExtension, "file64": checkData.file64, "contentType": checkData.contentType] as! [String : String])
}
print(datas)
我创建结构来添加数据,这是我的结构:
struct data {
var documentCode: String?
var attachmentSize: String?
var applicationType: String?
var fileExtension: String?
var file64: String
var contentType: String
}
然后我将一些字符串添加到结构中:
let allData = [
data(documentCode: "1", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6"),
data(documentCode: "a", attachmentSize: "b", applicationType: "c", fileExtension: "d", file64: "e", contentType: "f"),
data(documentCode: "12", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6")
]
这是我的循环,我 var datas = [String: String]()
将数据添加到字典:
override func viewWillAppear(_ animated: Bool) {
var datas = [String: String]()
let allData = [
data(documentCode: "1", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6"),
data(documentCode: "a", attachmentSize: "b", applicationType: "c", fileExtension: "d", file64: "e", contentType: "f"),
data(documentCode: "12", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6")
]
for checkData in allData{
datas = ["documentCode": checkData.documentCode, "applicationType": checkData.applicationType, "attachmentSize": checkData.attachmentSize, "fileExtension": checkData.fileExtension, "file64": checkData.file64, "contentType": checkData.contentType] as! [String : String]
print(datas)
}
print(datas)
}
当我在循环中打印数据时,我得到这样的数据:
["file64": "5", "applicationType": "3", "attachmentSize": "2", "fileExtension": "4", "contentType": "6", "documentCode": "1"]
["file64": "e", "fileExtension": "d", "documentCode": "a", "attachmentSize": "b", "contentType": "f", "applicationType": "c"]
["documentCode": "12", "fileExtension": "4", "attachmentSize": "2", "applicationType": "3", "file64": "5", "contentType": "6"]
但是当我在外部循环打印时,我只得到最后一个索引数组:
["file64": "5", "applicationType": "3", "attachmentSize": "2", "fileExtension": "4", "contentType": "6", "documentCode": "1"]
如何在循环外获取 allData
数组?
老实说,你似乎想要 allData
所以 print(allData)
.
为了解释您的示例,设置为 datas
的最后一项将是数组中的最后一个索引 ["file64": "5", "applicationType": "3", "attachmentSize": "2", "fileExtension": "4", "contentType": "6", "documentCode": "1"]
。
如果 datas
是一个位于循环内部的变量,print(datas)
的打印语句甚至不会工作。
我相信你只是想要print(allData)
。
在循环中,您使用相同的键更新了字典,结果字典中只有一个数据。要保存您需要使用字典数组的所有数据。 你可以像这样声明数据:
var datas : [[String: String]] = []
然后你可以在数组循环中追加:
for checkData in allData{
let data = ["documentCode": checkData.documentCode, "applicationType": checkData.applicationType, "attachmentSize": checkData.attachmentSize, "fileExtension": checkData.fileExtension, "file64": checkData.file64, "contentType": checkData.contentType] as! [String : String]
datas.append(data)
}
print(datas)
您刚刚创建了词典。你需要什么来创建“字典数组”。 更新代码:-
override func viewWillAppear(_ animated: Bool) {
var datas = [[String: String]]()
let allData = [
data(documentCode: "1", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6"),
data(documentCode: "a", attachmentSize: "b", applicationType: "c", fileExtension: "d", file64: "e", contentType: "f"),
data(documentCode: "12", attachmentSize: "2", applicationType: "3", fileExtension: "4", file64: "5", contentType: "6")
]
for checkData in allData{
datas.append(["documentCode": checkData.documentCode, "applicationType": checkData.applicationType, "attachmentSize": checkData.attachmentSize, "fileExtension": checkData.fileExtension, "file64": checkData.file64, "contentType": checkData.contentType] as! [String : String])
}
print(datas)