У меня есть этот код (MenuItem для меню гамбургеров SwiftUI, я использую массив MenuItem в цикле):
import Foundation
public class MenuItem
{
var title: String
var imagename: String
var index: Int
var action: () -> ()
init(title: String, index: Int, action: @escaping () -> ())
{
self.title = title
self.index = index
self.action = action
self.imagename = ""
}
convenience init(title: String, image: String, index: Int, action: @escaping () -> ())
{
self.init(title: title, index: index, action: action)
self.imagename = image
}
func getTitle() -> String
{
return self.title
}
}
//need in output "ItemTitle: Title 1"
var menuitems = [
MenuItem(title: "Title 1", index: 0, action: {
print("ItemTitle: \(menuitems[self.index])")//i can't compile it
}),
MenuItem(title: "Title 2", index: 1, action: {
print("ItemTitle: \(MenuItem.self.title)")//i can't compile it!
}),
MenuItem(title: "Title 3", index: 2, action: {
print("ItemTitle: \(self.getTitle())")//i can't compile it!
})]
Мне нужно использовать поля 'index', 'imagename' и 'title' в функции поле «действие». Как я могу это сделать?
PS: извините за мой Engli sh)))