0

我正在做一个项目,我正在尝试获取项目列表并将其返回到一个数组中以在我的项目中使用。

这是我的代码:

let identifier = delete.items

var identifiers: [String] = []
identifier.forEach({ (listModel) in
    identifiers = ["\(String(describing: listModel.remiderDate))"]
})

print("ITEMS DATES \(String(describing: identifiers.count))")

返回一个值数组而不是单独的identifier提醒日期,但我专注于获取reminderDate.

打印标识返回1,而数字预期返回 5。

4

1 回答 1

1

Your loop assigns a new value to identifiers each time so only the last value remains.

You probably want to use map:

let identifiers = delete.items.map { String(describing: $0.reminderDate) }
print("ITEMS DATES \(identifiers.count)")

Please note that using String(describing:) should only be used for debugging. Use a DateFormatter to convert a Date to a String.

于 2018-09-09T22:47:30.803 回答