我想将ItemID的值设为全局,以便我可以在函数旁边访问它。我试图在类下面定义它但它不起作用。我怎样才能在其他函数可以访问的函数中创建一个值?
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 itemID = metadataObj.stringValue!
let itemToSearchFor = itemID
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
的功能 func enterNewProduct() {
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
} })
self.dismiss(animated: true, completion: nil)
}
由于某种原因,它无法识别上一个函数的itemID
答案 0 :(得分:1)
将它放在像这样的函数上面。
var itemID:String!
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
然后在你的功能中你应该能够像这样访问它。
itemID = metadataObj.stringValue!
let itemToSearchFor = itemID
请务必使用var
以便修改。
答案 1 :(得分:0)
听起来你已经做好了一切。您忘记将代码放在“试图在类下面定义它”的位置。如果这是问题,这里有一个有效的模板:
class ViewController: UIViewController {
var myID = "ID #1"
override func viewDidLoad() {
super.viewDidLoad()
changeID()
printID()
}
func changeID() {
printID()
myID = "ID #2"
}
func printID() {
print(myID)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
控制台输出是:
ID #1
ID #2