gpt4 book ai didi

引用现有变量时出现 Swift 编译器错误

转载 作者:行者123 更新时间:2023-12-02 11:03:06 26 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Swift: expected declaration error setting "Label" to a string variable

(1 个回答)


7年前关闭。




我正在尝试为 UITextView 手动创建一些占位符文本,当我尝试设置占位符文本时,出现 Swift 编译器错误。 Xcode 告诉我它需要一个 pinContent 声明,我首先尝试设置它的文本值。这是我的代码:

class FirstViewController: UIViewController {

@IBOutlet var pinTitle: UITextField!

@IBOutlet var pinContent: UITextView!

@IBAction func createPin(sender: AnyObject) {



}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

println(userLocation)
}

// Manually create a placeholder for the text view
pinContent.text = "Description" // This line is where I get my error
pinContent.textColor = UIColor.lightGrayColor()

// Change the text properties of the text view when the user begins editing, so it types in the normal black font
func textViewDidBeginEditing(textView: UITextView) {
if textView.textColor == UIColor.lightGrayColor() {
textView.text = nil
textView.textColor = UIColor.blackColor()
}
}

// If the user leaves the text view blank when they're done editing, re-set the placeholder
func textViewDidEndEditing(textView: UITextView) {
if textView.text.isEmpty {
textView.text = "Description" // Sometimes I get the same error here as well
textView.textColor = UIColor.lightGrayColor()
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

最佳答案

问题似乎是您想将文本设置在方法或闭包的范围之外。试试这个:

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.
// Manually create a placeholder for the text view

pinContent.text = "Description" // This line is where I get my error
pinContent.textColor = UIColor.lightGrayColor()
println(userLocation)
}

关于引用现有变量时出现 Swift 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29323810/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com