Trying to sort an array using SwiftUI

Hi,

I'm trying to sort an array of organization names and phone numbers. When I try to use the normal sort() command, I get the error:


"Consecutive declarations on a line must be separated by ';'"


Here's my code (it's for the Apple Watch) but I am also trying to sort on an iPhone and get the same error.


import SwiftUI

struct ContentView: View {
    
    var choices = ["National Center for Missing and Exploted Children\n1-800-422-4453","National Domestic Violence Hotline\n1-800-799-7233","National Parents Hotline\n1-855-427-2736", "National Runaway Safeline\n1-800-786-2929"]
            
    var body: some View {
        
        List {
            
            ForEach(self.choices, id: \.self) { Options in
                
                    Text(Options)
                    .fontWeight(.bold)
                    .font(.caption)
                    .foregroundColor(Color .black)
                    .frame(height:110)
                    .listRowPlatterColor(Color.white)
            }
    }
            
    .listStyle(CarouselListStyle())
    
    .navigationBarTitle(Text("Child Abuse Phone"))
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            
            Group {
            ContentView()
                .previewDevice("Apple Watch Series 4")
        }
    }
}
}

Thanks for any help,

Dan Uff

Replies

Your code snippet doesn't show your attempt at sorting the array, but since you mention using

sort()
, I should point out that you're quite possibly using the wrong function.
sort()
is a method on a mutable collection that sorts the contents of that collection in-place and returns
Void
. Likely the version you want is
sorted()
, which will return a new collection containing the same contents but in sorted order:


struct ContentView: View {      
    var choices = [
      "National Center for Missing and Exploted Children\n1-800-422-4453",
      "National Domestic Violence Hotline\n1-800-799-7233",
      "National Parents Hotline\n1-855-427-2736",
      "National Runaway Safeline\n1-800-786-2929"
    ]
              
    var body: some View {
        List {
            ForEach(self.choices.sorted(), id: \.self) { opts in
                Text(opts)
                    .bold()
                    .font(.caption)  
                    .foregroundColor(.black)  
                    .frame(height:110)
                    .listRowPlatterColor(.white)
            }
        }
        .listStyle(CarouselListStyle())  
        .navigationBarTitle(Text("Child Abuse Phone"))
    }
}

Dan:


I'm not sure that your var-choice= can go at the top level of a class declaration like you have it. Try moving it inside a function declaration, instead. Might cure the error/get it out of the way of the sort (which I suspect is ok, but we won't know until you clear the error?), so it can complete.


Still no joy, might be yet another SUI bug...you know the drill.


Ken

I took out the sort() right before I posted the example above.


By the way, I tried the above in Playground, and it worked fine (with the sort() option).


Thanks,

Dan