[SOLVED] Swift/Xcode: Multiple Returns to Array


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?


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: "🍷")

Answered By – Dávid Pásztor

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *