简体   繁体   中英

Can i use 'for' loop on this?

I'm wondering if i may use 'for' loop for this code. Please forgive me, I know that is kind of a lame question, but I'm new to swift. Hope that you can help me here, guys!

Thanks a lot everyone!

Code:

override func viewDidLoad() {
    super.viewDidLoad()

    // Background color

    let kolorTla = UIColor(red: 0/255.0, green: 66/255.0, blue: 132/255.0, alpha: 1.0)
    view.backgroundColor = kolorTla

    // Icons border

    ramka.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
    ramka.layer.cornerRadius = 5.0
    ramka.layer.borderWidth = 3


    // Image

    skill1.image = UIImage(named: "english")

    // Image border

    skill1.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
    skill1.layer.cornerRadius = 5.0
    skill1.layer.borderWidth = 3
    skill1.contentMode = .scaleAspectFit

    // Image

    skill2.image = UIImage(named: "literature")

    // Image border

    skill2.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
    skill2.layer.cornerRadius = 5.0
    skill2.layer.borderWidth = 3
    skill2.contentMode = .scaleAspectFit

    // Image

    skill3.image = UIImage(named: "idea1")

    // Image border

    skill3.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
    skill3.layer.cornerRadius = 5.0
    skill3.layer.borderWidth = 3
    skill3.contentMode = .scaleAspectFit

Yes, you can. Place skill1,skill2, and skill3 in an array and iterate over it like this:

var objectArray = [skill1,skill2,skill3]


for object in objectArray
{
object.layer = ....


}

You can to some extent. You would just need to define an array of your items and loop over them. Not sure if it saves you much code wise but does make it a little more understandable.

override func viewDidLoad() {
    super.viewDidLoad()

    // Background color
    let kolorTla = UIColor(red: 0/255.0, green: 66/255.0, blue: 132/255.0, alpha: 1.0)
    view.backgroundColor = kolorTla

    // Icons border
    ramka.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
    ramka.layer.cornerRadius = 5.0
    ramka.layer.borderWidth = 3

    // Set Image
    skill1.image = UIImage(named: "english")
    skill2.image = UIImage(named: "literature")
    skill3.image = UIImage(named: "idea1")

    // Set Image border
    for skill in [skill1, skill2, skill3] {
        skill.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
        skill.layer.cornerRadius = 5.0
        skill.layer.borderWidth = 3
        skill.contentMode = .scaleAspectFit
    }
}

Better approach at least in my opinion would be to create a simple function to handle this instead.

override func viewDidLoad() {
    super.viewDidLoad()

    // Background color
    let kolorTla = UIColor(red: 0/255.0, green: 66/255.0, blue: 132/255.0, alpha: 1.0)
    view.backgroundColor = kolorTla

    // Icons border
    ramka.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
    ramka.layer.cornerRadius = 5.0
    ramka.layer.borderWidth = 3
    // Set Images
    setupImageView(imageView: skill1, imageName: "english")
    setupImageView(imageView: skill2, imageName: "literature")
    setupImageView(imageView: skill3, imageName: "idea1")
}

func setupImageView(imageView: UIImageView, imageName: String) {
    // Set Image
    imageView.image = UIImage(named: imageName)
    // Set Image border
    imageView.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
    imageView.layer.cornerRadius = 5.0
    imageView.layer.borderWidth = 3
    imageView.contentMode = .scaleAspectFit
}

While you're not able to use a for loop to modify the variable names and loop through them, you could put your skill instances in an array and loop through them that way.

...

skill1.image = UIImage(named: "english")
skill2.image = UIImage(named: "literature")
skill3.image = UIImage(named: "idea1")

let skills = [skill1, skill2, skill3]

for skill in skills {
    skill.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
    skill.layer.cornerRadius = 5.0
    skill.layer.borderWidth = 3
    skill.contentMode = .scaleAspectFit
}

You could just extend your ImageView

extension UIImageView {
   func addCustomLayer() { // add arguments to function if you wish to change the value assigned 
         self.layer.borderColor = UIColor(red: 255, green: 255, blue: 255, alpha: 1.0).cgColor
        self.layer.cornerRadius = 5.0
        self.layer.borderWidth = 3
        self.contentMode = .scaleAspectFit
    }
   } 
}

Then you call that method on each UIImageView

for each in [skill1, skill2, skill3] {
    each.addCustomLayer()
}

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