Clarence Clarence - 4 months ago 23
Swift Question

Image Name as variables(Swift)

I am trying to call a func as below, what should I write in the loadImage() so that I can type in different image name whenever I call it.

Many Thanks

func loadImage(????){

self.Picture.image = UIImage (named: "Ch1-4 new.jpg")

UIView.animateWithDuration(3.0,
delay: 0.0,
options: [],
animations : {

let message = "Picture.mp3"
let triggerTime = (Int64(NSEC_PER_SEC) * 1)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in
self.playMessageOpening(message)
})
self.Picture.hidden=false
self.Picture.alpha=0.0;
self.Picture.alpha=1.0;

},
completion: { finished in
print("Picutre done")


})
}

Answer
func loadImage(imageName: String) {
    self.Picture.image = UIImage (named: imageName)

    ...
}

Where imageName is the name of the parameter and String is the type. More on function declarations in the Apple docs.

Comments