0

Having some trouble with my code. So I have these HealthKit Objects I'm trying to return to be displayed as widgets later on in my code.

import Foundation

struct Activity: Identifiable {
   var id: String
   var name: String
   var image: String

    static func allActivities() -> [Activity] {
       return[Activity(id:"bloodAlcoholContent", name:"BAC: ", image: "🩸🍷")]
       return[Activity(id:"heartRate", name:"Heart Rate: ", image: "❤️ ")]
       return[Activity(id:"oxygenSaturation", name:"Blood Oxygen: ", image: "🩸")]
       return[Activity(id:"respiratoryRate", name:"Respiratory Rate: ", image: "🫁")]
       return[Activity(id:"numberOfAlcoholicBeverages", name:"Units Consumed: ", image: "🍷")]
    }
}

Xcode has warned me that this return value won't process code after it and upon building the app I can see what it meant after building the project. Only the first return value is given so only the BAC Widget is displayed. And the rest are not.

Any idea how I can do multiple returns?

1
  • What exactly do you want to do here? "Multiple returns" doesn't make sense? Do you want to return an array containing all of those activities? Then just use one return, and return [Activity(...), Activity(...), Activity(...), Activity(...), ...] and so on. Commented Mar 25, 2022 at 14:43

1 Answer 1

2

Your syntax is completely wrong. To create an Array, you need to include the elements separated by a comma in a single [].

You only need a single return statement for the Array itself (which you can actually omit if your function only contains a single expression).

static func allActivities() -> [Activity] {
    return [
            Activity(id:"bloodAlcoholContent", name:"BAC: ", image: "🩸🍷"),
            Activity(id:"heartRate", name:"Heart Rate: ", image: "❤️ "),
            Activity(id:"oxygenSaturation", name:"Blood Oxygen: ", image: "🩸"),
            Activity(id:"respiratoryRate", name:"Respiratory Rate: ", image: "🫁"),
            Activity(id:"numberOfAlcoholicBeverages", name:"Units Consumed: ", image: "🍷")
        ]
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.