简体   繁体   中英

How to convert an array of Strings to a list

I have an array of Strings and I want to use that as a list.

I followed this example but I didn't get for an array. Link

My current code is given below:

    struct ListOfPeripherals: Identifiable {
        var id = UUID()
        var peripheralName: String
    }

    struct RestaurantRow: View {
        var peripheralFromBLE: ListOfPeripherals

        var body: some View {

    // List to be implemented here

        }

    func getListOfAlphabets() -> [String] {
        let listOfAlphabets = [A,B,C,D,E]
        return listOfAlphabets
    }
  }

You dont have to convert your Array of String objects to make the List work. You should describe your error pasting it along with your code to get helped faster. So, this is my assumption of your concept of error.

The Array type is fine, but however, his Elements, in this case the strings, need to be identified. This means you have to conform your String to the Identifiable protocol, and providing an id property which must be unique (a simple UUID object will do the trick).

In your code, the function getListOfAlphabets returns an array of Strings which cannot be used. If you were to use a [ListOfPeripherals] array that would work because it conforms to the protocol. I think you got a bit of confusion and you are basically returning an array of wrong type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM