Home > Net >  Value of type 'NSSet' has no member '_' - Unable to Access attributes through NS
Value of type 'NSSet' has no member '_' - Unable to Access attributes through NS

Time:12-06

I have a core data model set up with 3 entities: Exercise, ExerciseSet, and Workout. It's set up so that Exercise and ExerciseSet have a one to one relationship and Workout and ExerciseSet have a one to many relationship. I am trying to access the properties within ExerciseSet via Workout but when I run this code:

struct WorkoutView: View {
@State var workout = Workout()
    
    var body: some View {
        
        Text("Sets in Workout: \(workout.exerciseSets.count)")
        
        ForEach(Array(arrayLiteral: workout.exerciseSets), id: \.self) { e in
            Text("Exercise Name: \(e.exercise.exercisename), Reps: \(e.reps), Weight: \(e.weight)")         
        }

I get the following error

Value of type 'NSSet' has no member 'exercise'
Value of type 'NSSet' has no member 'reps'
Value of type 'NSSet' has no member 'weight'

These properties all exist when I call them outside of the workout and workout.exerciseSets.count returns as expected.

Here are the definitions of each entity:

extension Exercise {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Exercise> {
        return NSFetchRequest<Exercise>(entityName: "Exercise")
    }

    @NSManaged public var exercisename: String
    @NSManaged public var id: UUID?
    @NSManaged public var musclegroup: String?
    @NSManaged public var exerciseSet: ExerciseSet?

}

extension Exercise : Identifiable {

}
extension ExerciseSet {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<ExerciseSet> {
        return NSFetchRequest<ExerciseSet>(entityName: "ExerciseSet")
    }

    @NSManaged public var dateCompleted: Date
    @NSManaged public var id: UUID?
    @NSManaged public var reps: Int16
    @NSManaged public var weight: Int16
    @NSManaged public var exercise: Exercise
    @NSManaged public var workout: Workout?

}

extension ExerciseSet : Identifiable {

}

extension Workout {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Workout> {
        return NSFetchRequest<Workout>(entityName: "Workout")
    }

    @NSManaged public var id: UUID
    @NSManaged public var notes: String?
    @NSManaged public var workoutDate: Date
    @NSManaged public var workoutMuscleGroup: String?
    @NSManaged public var workoutName: String?
    @NSManaged public var exerciseSets: NSSet
    
    public var exerciseSetArray: [ExerciseSet] {
        let set = exerciseSets as? Set<ExerciseSet> ?? []
        return set.sorted {
            $0.dateCompleted < $1.dateCompleted
        }
        
    }

}

// MARK: Generated accessors for exerciseSets
extension Workout {

    @objc(addExerciseSetsObject:)
    @NSManaged public func addToExerciseSets(_ value: ExerciseSet)

    @objc(removeExerciseSetsObject:)
    @NSManaged public func removeFromExerciseSets(_ value: ExerciseSet)

    @objc(addExerciseSets:)
    @NSManaged public func addToExerciseSets(_ values: NSSet)

    @objc(removeExerciseSets:)
    @NSManaged public func removeFromExerciseSets(_ values: NSSet)

}

extension Workout : Identifiable {

}

Is there something that I am missing in order to be able to access the attributes within ExerciseSet through Workout? Thank you.

CodePudding user response:

NSSet in Swift has no idea of the underlying type.

Replace Array(arrayLiteral: workout.exerciseSets) with workout.exerciseSetArray.

It's mandatory to convert NSSet to native Set<ExerciseSet> to be able to access the attributes. Or - easier - declare exerciseSets as Set<ExerciseSet>. Core Data works pretty well with native Swift types.

  • Related