【发布时间】:2021-02-02 08:48:35
【问题描述】:
我有一个来自服务器的数组。
我的问题是当我想将对象添加到 大批。如何解决此问题并使用对象数组 来自服务器??
返回数据示例
[
{
"empId": 1970083,
"empCode": "2007",
"empName": "Emp Test",
"monthClosed": 0,
"monthApproved": 0,
"approvedDate": 0,
"employerName": "Name",
"employerApproval": 1,
"employerApprovalDate": "2020-09-02 17:22:51.843"
},
]
这个结构来接收这个数据
struct GetMonthSummaryObj: Codable {
var empId: Int?
var empCode: String?
var empName: String?
var monthClosed: Int?
var monthApproved: Int?
var approvedDate: Int?
var employerName: String?
var employerApproval: Int?
var employerApprovalDate: String?
}
这是 ViewModel 中的一种方法,用于添加来自数组的数据并将其用于表。例如,显示或表格中的单元格数
func setEmployees(employees: [GetMonthSummaryObj?]) {
employeesList = []
employees.forEach {_ in
employeesList.append(CloseMonthEmpListItem(employee: employees))
//Error - Cannot convert value of type '[GetMonthSummaryObj?]' to expected argument type 'GetMonthSummaryObj'
}
}
在这里我创建了一个数组对象来使用它
class CloseMonthEmpListItem: Equatable, NSCopying {
var employee: GetMonthSummaryObj
init(employee: GetMonthSummaryObj) {
self.employee = employee
}
static func == (lhs: CloseMonthEmpListItem, rhs: CloseMonthEmpListItem) -> Bool {
return lhs.employee.empId == rhs.employee.empId
}
func copy(with zone: NSZone? = nil) -> Any {
let copy = CloseMonthEmpListItem(employee: employee)
return copy
}
}
【问题讨论】:
标签: ios arrays json swift mvvm