2016-11-21 54 views
0

我想使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) 

} 

由於某種原因,它不是從以前的功能

+0

什麼「沒有工作」呢? – Wez

+0

它沒有獲取itemID。我修改了enterNewItemID函數來打印ItemID,但沒有出現。 –

回答

1

承認ITEMID把它上面的函數的地方像這樣的功能。

var itemID:String! 
func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) { 

然後在你的函數中,你應該可以像這樣訪問它。

itemID = metadataObj.stringValue! 
let itemToSearchFor = itemID 

確保使用var,以便您可以對其進行修改。

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 
相關問題