I’ve a dictionary outlined as the next
@State var instructorNamesDictionary: [String: [String]] = [:]
Content material is added to the dictionary in a .activity later
for course in programs{
var updatedinstructors: [InstructorModel] = []
for instructorID in course.instructors {
let teacher = attempt await getInstructor(uid: instructorID)
updatedinstructors.append(contentsOf: teacher)
for names in updatedinstructors{
instructorNamesDictionary[instructorID] = [names.name]
}
}
}
Afterwards I attempt to use this dictionary right into a struct known as CourseCard that takes in some parameters:
struct CoursesCard: View {
var title: String
var picture: Picture
var youtubeLinks: [String]
var description: String
var instructors: [String]
var labels: [String]
var isFree: Bool
var ispro: Bool
//remainder of design code
}
Now after I do a ForEach loop on one other array by means of
ForEach(basicCourses){course in
let picture = photographs[course.image_ref] ?? Picture("noImage")
CoursesCard(title: course.Title, picture: picture, youtubeLinks: course.youtube_videos, description: course.Description, instructors: instructorNamesDictionary[
ForEach(course.instructors){instruc in
instruc}] ?? ["No instructor"], labels: course.labels, isFree: course.isfree, ispro: isPro)
This offers me “Can’t convert worth ‘basicCourses’ of kind ‘[CourseModel]’ to anticipated kind ‘Binding<[CourseModel]>’, use wrapper as an alternative”
Nevertheless if I go one other array, no error is given.
Right here is CourseModel and InstructorModel if wanted:
struct CourseModel: Decodable,Identifiable {
let id: String
let Title: String
let Description: String
let image_ref: String
let youtube_videos: [String]
let labels: [String]
let instructors: [String]
let isfree: Bool
enum CodingKeys: String, CodingKey {
case id
case Title
case Description
case image_ref
case youtube_videos
case labels
case instructors
case isfree
}
}
struct InstructorModel: Decodable,Identifiable {
let id: String
let title: String
enum CodingKeys: String, CodingKey {
case id
case title
}
}
I attempted to vary the ForEach loop into
ForEach($basicCourses){$course in
let picture = photographs[course.image_ref] ?? Picture("noImage")
CoursesCard(title: course.Title, picture: picture, youtubeLinks: course.youtube_videos, description: course.Description, instructors: instructorNamesDictionary[
ForEach(course.instructors){instruc in
instruc}] ?? ["No instructor"], labels: course.labels, isFree: course.isfree, ispro: isPro)
however that simply throws out different errors.