UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Phonebook

Forums > SwiftUI

@KURD  

Hello to all How can I index people on the list? To put the edit button and the favorit items for the list This method gives me an error : Fatal error: Unexpectedly found nil while unwrapping an Optional value

import SwiftUI

struct EditContact: View {

@Environment
(\.presentationMode) var presentationMode:Binding<PresentationMode>

@ObservedObject
 var CON : Contact

@State
 var act = false

    // id = UUID()
    var char : charContact
    var index: Int {
        http://CON.contact.firstIndex(where: { $0.id == http://char.id })!
    }

@State
 var active = false

    var body: some View {
        NavigationView {
            VStack {
                Image(systemName: "http://camera.circle.fill")
                    .padding()
                    .padding()
                    .padding()
                    .font(.system(size: 180))
                Spacer()
                VStack(alignment: .leading) {
                    HStack {
                        Text("Name")
                        Spacer()
                        TextField("\(http://self.char.name)", text: $CON.contact[self.index].name)
                            .padding()
                            .frame(width: 250, height: 40)
                            .border(Color.white, width: 1)
                    }.padding()
                    HStack {
                        Text("Title")
                        Spacer()
                        TextField("\(self.char.title)", text: $CON.contact[self.index].title)
                            .padding()
                            .frame(width: 250, height: 40)
                            .border(Color.white, width: 1)
                    }.padding()
                    HStack {
                        Text("Company")
                        Spacer()
                        TextField("\(http://self.char.company)", text: $CON.contact[self.index].company)
                            .padding()
                            .frame(width: 250, height: 40)
                            .border(Color.white, width: 1)
                    }.padding()
                    HStack {
                        Text("Email")
                        Spacer()
                        TextField("\(http://self.char.email)", text: $CON.contact[self.index].email)
                            .padding()
                            .frame(width: 250, height: 40)
                            .border(Color.white, width: 1)
                    }.padding()

                }
                Rectangle()
                    .frame(minWidth: 0, maxWidth: 380, maxHeight: 1)

                HStack {
                    Text("Mobile")
                    Spacer()
                    TextField("\(http://self.char.mobile)", text: $CON.contact[self.index].mobile)
                        .padding()
                        .frame(width: 250, height: 40)
                        .border(Color.white, width: 1)
                }.padding()

                Spacer()
                HStack {
                Button(action: {
                    if  http://self.CON.contact[self.index].name == "" && http://self.CON.contact[self.index].title == "" && http://self.CON.contact[self.index].company == "" && http://self.CON.contact[self.index].mobile == "" {
                        self.act = true
                    }
                }){
                    Text("Save")
                        .padding()
                        .frame(width: 150, height: 40)
                        .border(Color.white, width: 2)
                        .foregroundColor(.orange)
                }
                .padding()
                    Button(action: {
                        self.presentationMode.wrappedValue.dismiss()
                    }){
                        Text("Back")
                        .padding()
                        .frame(width: 150, height: 40)
                        .border(Color.white, width: 2)
                        .foregroundColor(.orange)
                    }
                }
                .alert(isPresented: $act) {
                    Alert(title: Text("Warning!!"), message: Text("Please enter the details you want to edit or press the Back button."))
                }

            }.navigationBarTitle("Edit contact",displayMode: .inline)

        }.colorScheme(.dark)
    }
}
var index: Int {
        http://CON.contact.firstIndex(where: { $0.id == http://char.id })!
    }

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.