swift 中的函数错误
Function Error in swift
我正在尝试在此函数中调用 UIWebView,但出现错误。我怎样才能修复这个错误?有没有办法从它的函数中调用一个变量?我这样做的原因是因为我的 webViewDidFinishLoad 改变了我在 UIWebView 的高度。我正在尝试在其下方加载一个 UIImage。
func webViewDidFinishLoad (jobDescView : UIWebView){
// Change the height dynamically of the UIWebView to match the html content
var jobDescViewFrame: CGRect = jobDescView.frame
jobDescViewFrame.size.height = 1
jobDescView.frame = jobDescViewFrame
var fittingSize: CGSize = (jobDescView.sizeThatFits(CGSizeZero))
jobDescViewFrame.size = fittingSize
// webViewFrame.size.width = 276; Making sure that the webView doesn't get wider than 276 px
jobDescView.frame = jobDescViewFrame
var jobDescViewHeight = jobDescView.frame.size.height
}
func bar2View (bar2View: UIImageView){
var jobDescViewHeight = jobDescView.frame.size.height -->>ERROR unresolved identifier 'jobDescView'
}
这与变量范围有关:
由于您的变量是在函数内部设置的,因此您无法从其他函数访问它。
将 var jobDescViewFrame 设置为 class 变量
然后 jobDescViewFrame = jobDescView.frame
class MyClass : OptionalSuperClass, OptionalProtocol1 {
var jobDescViewHeight:CGFloat!
func webViewDidFinished(jobDescView:UIWebView){
jobDescViewFrame = jobDescView.frame
}
func bar2View (bar2View: UIImageView){
jobDescViewHeight = jobDescView.frame.size.height '
}
我正在尝试在此函数中调用 UIWebView,但出现错误。我怎样才能修复这个错误?有没有办法从它的函数中调用一个变量?我这样做的原因是因为我的 webViewDidFinishLoad 改变了我在 UIWebView 的高度。我正在尝试在其下方加载一个 UIImage。
func webViewDidFinishLoad (jobDescView : UIWebView){
// Change the height dynamically of the UIWebView to match the html content
var jobDescViewFrame: CGRect = jobDescView.frame
jobDescViewFrame.size.height = 1
jobDescView.frame = jobDescViewFrame
var fittingSize: CGSize = (jobDescView.sizeThatFits(CGSizeZero))
jobDescViewFrame.size = fittingSize
// webViewFrame.size.width = 276; Making sure that the webView doesn't get wider than 276 px
jobDescView.frame = jobDescViewFrame
var jobDescViewHeight = jobDescView.frame.size.height
}
func bar2View (bar2View: UIImageView){
var jobDescViewHeight = jobDescView.frame.size.height -->>ERROR unresolved identifier 'jobDescView'
}
这与变量范围有关:
由于您的变量是在函数内部设置的,因此您无法从其他函数访问它。
将 var jobDescViewFrame 设置为 class 变量
然后 jobDescViewFrame = jobDescView.frame
class MyClass : OptionalSuperClass, OptionalProtocol1 {
var jobDescViewHeight:CGFloat!
func webViewDidFinished(jobDescView:UIWebView){
jobDescViewFrame = jobDescView.frame
}
func bar2View (bar2View: UIImageView){
jobDescViewHeight = jobDescView.frame.size.height '
}