Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading