self 在 swift 项目中不可用
self is not available in swift project
我正在尝试 运行 在 swift 中进行图像比较。但是我收到一条错误消息,指出 Cannot use instance member 'featureprintObservationForImage' within property initializer; property initializers run before 'self' is available
。
错误出现在 let
行的第一行。
为什么 self 在 ViewController 中不可用?
import UIKit
import Vision
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func featureprintObservationForImage(atURL url: URL) -> VNFeaturePrintObservation? {
let requestHandler = VNImageRequestHandler(url: url, options: [:])
let request = VNGenerateImageFeaturePrintRequest()
do {
try requestHandler.perform([request])
return request.results?.first as? VNFeaturePrintObservation
} catch {
print("Vision error: \(error)")
return nil
}
}
let apple1 = featureprintObservationForImage(atURL:Bundle.main.url(forResource:"apple1", withExtension: "jpg")!)
let apple2 = featureprintObservationForImage(atURL: Bundle.main.url(forResource:"apple2", withExtension: "jpg")!)
let pear = featureprintObservationForImage(atURL: Bundle.main.url(forResource:"pear", withExtension: "jpg")!)
var distance = Float(0)
try apple1!.computeDistance(&distance, to: apple2!)
var distance2 = Float(0)
try apple1!.computeDistance(&distance2, to: pear!)
}
featureprintObservationForImage
函数右大括号后的所有代码都不在任何函数或闭包中。你不能那样做。 (该代码不仅仅是变量声明。您有函数调用,这在函数之外是不合法的。)
您 可以 创建称为计算属性的东西,您可以在其中提供一个闭包,每次您从 属性.
读取值时都会调用该闭包
您不能在变量声明中访问 self
。
import UIKit
import Vision
class ViewController: UIViewController {
var testVeriable : Int = 0//hear you cannot access `self`
func test(){
self.testVeriable = 1// In function you can access class variable or function using `self`
}
}
我正在尝试 运行 在 swift 中进行图像比较。但是我收到一条错误消息,指出 Cannot use instance member 'featureprintObservationForImage' within property initializer; property initializers run before 'self' is available
。
错误出现在 let
行的第一行。
为什么 self 在 ViewController 中不可用?
import UIKit
import Vision
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func featureprintObservationForImage(atURL url: URL) -> VNFeaturePrintObservation? {
let requestHandler = VNImageRequestHandler(url: url, options: [:])
let request = VNGenerateImageFeaturePrintRequest()
do {
try requestHandler.perform([request])
return request.results?.first as? VNFeaturePrintObservation
} catch {
print("Vision error: \(error)")
return nil
}
}
let apple1 = featureprintObservationForImage(atURL:Bundle.main.url(forResource:"apple1", withExtension: "jpg")!)
let apple2 = featureprintObservationForImage(atURL: Bundle.main.url(forResource:"apple2", withExtension: "jpg")!)
let pear = featureprintObservationForImage(atURL: Bundle.main.url(forResource:"pear", withExtension: "jpg")!)
var distance = Float(0)
try apple1!.computeDistance(&distance, to: apple2!)
var distance2 = Float(0)
try apple1!.computeDistance(&distance2, to: pear!)
}
featureprintObservationForImage
函数右大括号后的所有代码都不在任何函数或闭包中。你不能那样做。 (该代码不仅仅是变量声明。您有函数调用,这在函数之外是不合法的。)
您 可以 创建称为计算属性的东西,您可以在其中提供一个闭包,每次您从 属性.
读取值时都会调用该闭包您不能在变量声明中访问 self
。
import UIKit
import Vision
class ViewController: UIViewController {
var testVeriable : Int = 0//hear you cannot access `self`
func test(){
self.testVeriable = 1// In function you can access class variable or function using `self`
}
}