How can i insert a value of textfield texts into an array of type class?
Basically, i have two view controllers one shows the list of items and code created using the following class
class Courses {
var courseName : String
var courseCode : Int
init(courseName : String, courseCode : Int){
self.courseName = courseName
self.courseCode = courseCode
}
}
//created array from that class
var courses : [Courses] = [Courses(courseName: "Unix Linux", courseCode: 101),
Courses(courseName: "ASP.Net", courseCode: 202),
Courses(courseName: "CISCO", courseCode: 203),
Courses(courseName: "Photoshop Editing", courseCode: 306)
]
Second View Controller has two text field which will allow someone to add new courses to the existing array. How can i achieve that and reload the table view with new items from a different view controller
i added var courses : [Courses] = [] on the second view controller.swift file. not sure what else to do. I tried something like this
@IBAction func addCourseButton(_ sender: Any) {
if courseName.text != "" {
let courseCodeString = Int(courseCode.text!)
let item = Courses(courseName: "\(courseName.text)", courseCode: courseCodeString!)
courses.append(item)
for i in courses {
print(i.courseName)
}
}
}