Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Set to Array?

When retrieving a collection of objects from CoreData via a relationship, swift is giving me an NSSet rather than an array as i would have expected.

Is there a way i can convert the set into an array?

Code:

var updateExercise : UserExercise?

destinationViewController?.userExerciseSets = self.updateExercise?.exercisesets as? [UserExerciseSet]

the cautions are

Cast from 'NSSet?' to unrelated type '[UserExerciseSet]' always fails

Destination VC has the var : var userExerciseSets : [UserExerciseSet]?

like image 617
jwarris91 Avatar asked Nov 29 '25 14:11

jwarris91


1 Answers

You should define your NSManagedObject model as such:

class UserExercise: NSManagedObject {
   @NSManaged var exercises: Set<Exercise>!
}

Then when you need an array, you can simply use the Array's constructor that takes a set.

let exercises = Array(userExercise.exercises)
like image 52
jimj Avatar answered Dec 02 '25 04:12

jimj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!