jeudi 20 juillet 2017

Refactor code advice for iOS using Swift 3 or 4

I have created some code to that puts a picked image from a image picker that puts the images into an array.

I want to then put the images from the array into another variable that is used to upload to firebase. The problem I'm having is if the user doesn't pick 8 pictures then it will crash the program because the new variables are expecting them, so I have written some If, Else code but its long doing lots of checks to the count of images in the array. I am looking to see if this can be refactored maybe a switch statement if something. I am fairly new so maybe I have the wrong idea about what a switch can do. I am going to read more now about them.

Below is my code.

func doneButtonDidPress(_ imagePicker: ImagePickerController, images: [UIImage]) {
    if images.count > 8  {

        imagePicker.dismiss(animated: true, completion: nil)

        photoNumber()

    }else {
        if images.count == 1 {
    photo1 = images[0]
        }else {
            print("Tony: No image in number 1")
        }
            if images.count == 2 {
    photo1 = images[0]
    photo2 = images[1]
            }else {
                print("Tony: No image in number 2")
            }
        if images.count == 3 {
            photo1 = images[0]
            photo2 = images[1]
            photo3 = images[2]
        }else {
            print("Tony: No image in number 3")
        }
        if images.count == 4 {
            photo1 = images[0]
            photo2 = images[1]
            photo3 = images[2]
            image4 = images[3]
        }else {
            print("Tony: No image in number 4")
        }
        if images.count == 5 {
            photo1 = images[0]
            photo2 = images[1]
            photo3 = images[2]
            image4 = images[3]
            image5 = images[4]
        }else {
            print("Tony: No image in number 5")
        }
        if images.count == 6 {
            photo1 = images[0]
            photo2 = images[1]
            photo3 = images[2]
            image4 = images[3]
            image5 = images[4]
            image6 = images[5]
        }else {
            print("Tony: No image in number 6")
        }
        if images.count == 7 {
            photo1 = images[0]
            photo2 = images[1]
            photo3 = images[2]
            image4 = images[3]
            image5 = images[4]
            image6 = images[5]
            image7 = images[6]
        }else {
            print("Tony: No image in number 7")
        }
        if images.count == 8 {
            photo1 = images[0]
            photo2 = images[1]
            photo3 = images[2]
            image4 = images[3]
            image5 = images[4]
            image6 = images[5]
            image7 = images[6]
            image8 = images[7]
        }else {
            print("Tony: No image in number 8")
        }

    imagePicker.dismiss(animated: true, completion: nil)
 }
} 

Aucun commentaire:

Enregistrer un commentaire