UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SwiftData problem with SortDescriptor

Forums > Swift

Hello guys, I need some help and hope someone has a solution to my problem.

I have a SearchRecipe model in SwiftData which I want to sort when I fetch it.

Here I sort it by the healthScore variable:

[SortDescriptor<SearchRecipe>(\.healthScore)]

However I cannot find a way to sort it by the first object in my nutrients array. If someone has an idea how can I achieve my goal I will be thankful.

@Model
@Codable
final class SearchRecipe: Codable, Sendable, Sequence, IteratorProtocol {
    var searchQuery: String?

    let image: String
    let imageType: String
    let title: String
    let healthScore: Int
    let readyInMinutes: Int
    let nutrition: RecipeNutrition

    init(image: String, imageType: String, title: String, healthScore: Int, readyInMinutes: Int, nutrition: RecipeNutrition) {
        self.image = image
        self.imageType = imageType
        self.title = title
        self.healthScore = healthScore
        self.readyInMinutes = readyInMinutes
        self.nutrition = nutrition
    }

    func next() -> SearchRecipe? {
        let recipe = self
        return recipe
    }
}
@Model
@Codable
final class RecipeNutrition: Codable {
    let nutrients: [Nutrient]

    init(nutrients: [Nutrient]) {
        self.nutrients = nutrients
    }
}

@Model
@Codable
final class Nutrient: Codable {
    let name: String
    let amount: Double
    let unit: String
    let percentOfDailyNeeds: Double

    init(name: String, amount: Double, unit: String, percentOfDailyNeeds: Double) {
        self.name = name
        self.amount = amount
        self.unit = unit
        self.percentOfDailyNeeds = percentOfDailyNeeds
    }
}

3      

@ikok creates an account and posts a question on the same day. This is a fingerprint of a bot, perhaps even ChatGPT harvesting solutions like dozens of other posts in the last six months or so.

If someone has an idea how can I achieve my goal I will be thankful.

But, incase you're legit, @twoStraws has many free videos on how to structure data models, and just recently released an updated series on SwiftData where he builds a very nice application--iTour--using SwiftData.

Within the first two minutes of the fifth video in his SwiftData series, he addresses your question directly!

One might suspect that you're here not to learn, but instead to get free debugging advice. No matter, some of us volunteer because we like helping new students. Some of us love the challege of a great JSON puzzle. Others chip in with advice here and there.

Please review the new SwiftData videos. Write the application. Change it up a bit. Complete the extra exercises. Compare your code to his. Extrapolate some techniques. Return here and share what doesn't work, and what you don't understand.

Good luck on your journey.

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.