Dezkiir
Dezkiir

Reputation: 29

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?

Upvotes: 0

Views: 58

Answers (1)

David Pasztor
David Pasztor

Reputation: 54706

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

Upvotes: 2

Related Questions