首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何从一个函数中获取值并在swift中的另一个函数中使用它

如何从一个函数中获取值并在swift中的另一个函数中使用它
EN

Stack Overflow用户
提问于 2016-11-17 01:05:11
回答 1查看 77关注 0票数 0

我有这个功能

代码语言:javascript
运行
复制
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中,但它不起作用。

代码语言:javascript
运行
复制
  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中被标记为未解析的标识符。我已经被困在这件事上很久了。请告诉我任何关于如何解决这个问题的想法。

EN

回答 1

Stack Overflow用户

发布于 2016-11-17 02:34:30

尝试在任何函数的外部声明类内的itemId变量。这将使变量对类中的任何函数都可用。

代码语言:javascript
运行
复制
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

 }


} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40638066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档