Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift:将NSObject强制转换为其他类型的UI对象_Ios_Swift - Fatal编程技术网

Ios Swift:将NSObject强制转换为其他类型的UI对象

Ios Swift:将NSObject强制转换为其他类型的UI对象,ios,swift,Ios,Swift,假设函数myFunc(发送方:NSObject)获取并输入对象: func myFunc(sender:NSObject){ if sender is UILabel { println("i'm label") }else{ println("i'm not!") } } @IBAction func runMyFunc(sender: UIButton) { myFunc(myLabel) } 我已经将ui按钮、ui图像和ui标签连接到我的代

假设函数
myFunc(发送方:NSObject)
获取并输入对象:

func myFunc(sender:NSObject){
   if sender is UILabel {
      println("i'm label")
   }else{
      println("i'm not!")
   }
}
@IBAction func runMyFunc(sender: UIButton) {
   myFunc(myLabel)
}
我已经将
ui按钮
ui图像
ui标签
连接到我的代码:

@IBOutlet var myLabel: UILabel!
@IBOutlet var myImage: UIImage!
@IBOutlet var myButton: UIButton!
并且
myButton
运行
runMyFunc()

上面的函数运行得很好,我正在尝试这样做:

func myFunc(sender:NSObject){
       if sender is UILabel {
          sender.text = "i'm new lable"
       }else{
          sender.image = UIImage(named: "newImage.png")
       }
}
但是它不起作用,当发送者是
UILabel
时,如何告诉编译器,将其转换为
UILabel

这应该起作用:

if let tempLabel = sender as? UILabel {
    tempLabel.text = "i am a label"

} else if let tempImageView = sender as? UIImageView {
   tempImageView.image = UIImage(named: "newImage.png")
}