我有这个功能
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
// Check if the metadataObjects array is not nil and it contains at least one object.
if metadataObjects == nil || metadataObjects.count == 0 {
qrCodeFrameView?.frame = CGRect.zero
messageLabel.text = "No QR/barcode is detected"
return
}
//Get metadata object
let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject
if supportedCodeTypes.contains(metadataObj.type) {
//if the found metadata is equal to the QR code metadata then update the status label's text and set the the bounds
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj)
qrCodeFrameView?.frame = barCodeObject!.bounds
if metadataObj.stringValue != nil {
messageLabel.text = metadataObj.stringValue
//Searches firebase for existing barcode
}
let itemToSearchFor = metadataObj.stringValue
let itemID = metadataObj.stringValue
guard let Description = productDescriptionTextField.text,
let price = priceTextField.text,
let location = productLocationTextField.text
else{
print("Fill basic product information")
return
}
let ref = FIRDatabase.database().reference(fromURL: "")
// creating an item child node
let values = ["Item Description": Description, "Image": price, "Location": location, "Price": price ]
let items = ref.child("Items").child(itemID!)
items.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err)
return
} })
FIRDatabase.database().reference().child("Items").child(itemToSearchFor!).observeSingleEvent(of: .value, with:{(snap) in
print(snap)
})
}}
如何从上面的函数中获取“itemID”的值,并在下面的函数中使用它。我试图将函数captureOuput嵌套在函数enterNewProduct中,但它无法正常工作。
func enterNewProduct() {
// This is my attempt to nest the function captureOutput inside function enterNewProduct but its not working.
// func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!){
return itemID
}
guard let Description = self.productDescriptionTextField.text,
let price = self.priceTextField.text,
let location = self.productLocationTextField.text
else{
print("Fill basic product information")
return
}
let ref = FIRDatabase.database().reference(fromURL: " /")
// creating an item child node
let values = ["Item Description": Description, "Image": price, "Location": location, "Price": price ]
let items = ref.child("Items").child(itemID)
items.updateChildValues(values, withCompletionBlock: { (err, ref) in
if err != nil {
print(err)
return
}
})
我之所以考虑将来自captureOutput的值嵌套或导入到enterNewProduct中的原因是因为我需要在enterNewProduct中使用itemID(已在captureOutput中定义),但它在enterNewProduct中被标记为未解析的标识符。我已经坚持了很长时间。请让我知道如何解决这个问题。
答案 0 :(得分:0)
尝试在任何函数之外的Class中声明itemId变量。这将使变量可用于类中的任何函数。
class MainViewController: UIViewController {
var itemId: String! = String()
func captureOutput() {
print(self.itemId)
//will print whatever the current string value of itemId is
}
func enterNewProduct() {
print(self.itemId)
//will print whatever the current string value of itemId is
}
func otherFunctions() {
print(self.itemId)
//will print whatever the current string value of itemId is
}
}