如何在核心数据swift中设置条形码的值

时间:2015-04-18 20:49:56

标签: swift core-data barcode

我有一个代码在读取后被条形码读取我想要放入核心数据的值

此输出条形码

func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection 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 = CGRectZero

            return
        }

        // Get the metadata object.
        let metadataObj = metadataObjects[0] as! AVMetadataMachineReadableCodeObject

        // Here we use filter method to check if the type of metadataObj is supported
        // Instead of hardcoding the AVMetadataObjectTypeQRCode, we check if the type
        // can be found in the array of supported bar codes.
        if supportedBarCodes.filter({ $0 == metadataObj.type }).count > 0 {
            // If the found metadata is equal to the QR code metadata then update the status label's text and set the bounds
            let barCodeObject = videoPreviewLayer?.transformedMetadataObjectForMetadataObject(metadataObj as AVMetadataMachineReadableCodeObject) as! AVMetadataMachineReadableCodeObject
            qrCodeFrameView?.frame = barCodeObject.bounds

            println(metadataObj.stringValue)

        }

    }

此核心数据代码

let newItem = NSEntityDescription.insertNewObjectForEntityForName("Codes", inManagedObjectContext: self.managedObjectContext!) as! Codes

            newItem.code = metadataObj.stringValue

2 个答案:

答案 0 :(得分:0)

您需要将更改保存到Core Data,例如:

context.save(nil)

其中context是您的NSManagetObjectContext。

您应该检查本地SQL lite数据库 - 常见的sqlite Viewer对检查CoreData更改非常有帮助。

答案 1 :(得分:0)

将变量名称分配给metadataObj.stringValue,如var a = metadataObj.stringValue,然后将其保存在持久化上下文中