0
你好,我想輸入代碼到核心數據表中,通過掃描條形碼來獲得這段代碼,以及如何在將代碼插入到數據庫之前使用匹配的數組代碼進行編碼,然後在應用程序運行時需要一個條件第二次審查現有數據庫中的代碼如何通過條形碼代碼將數據輸入到CoreData中?
條形碼代碼
func barcodefunc(){
// Get an instance of the AVCaptureDevice class to initialize a device object and provide the video
// as the media type parameter.
let captureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
// Get an instance of the AVCaptureDeviceInput class using the previous device object.
var error:NSError?
let input: AnyObject! = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: &error)
if (error != nil) {
// If any error occurs, simply log the description of it and don't continue any more.
println("\(error?.localizedDescription)")
return
}
// Initialize the captureSession object.
captureSession = AVCaptureSession()
// Set the input device on the capture session.
captureSession?.addInput(input as AVCaptureInput)
// Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
let captureMetadataOutput = AVCaptureMetadataOutput()
captureSession?.addOutput(captureMetadataOutput)
// Set delegate and use the default dispatch queue to execute the call back
captureMetadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
captureMetadataOutput.metadataObjectTypes = supportedBarCodes
// Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
videoPreviewLayer?.frame = view.layer.bounds
view.layer.addSublayer(videoPreviewLayer)
// Move the message label to the top view
view.bringSubviewToFront(meseg)
// Initialize QR Code Frame to highlight the QR code
qrCodeFrameView = UIView()
qrCodeFrameView?.layer.borderColor = UIColor.greenColor().CGColor
qrCodeFrameView?.layer.borderWidth = 2
view.addSubview(qrCodeFrameView!)
view.bringSubviewToFront(qrCodeFrameView!)
}
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
meseg.text = "No QR code is detected"
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
if metadataObj.stringValue != nil {
meseg.text = metadataObj.stringValue
}
}
}
此coredata代碼
let entityDescripition = NSEntityDescription.entityForName("Usercode", inManagedObjectContext: managedObjectContext!)
let task = Usercode(entity: entityDescripition!, insertIntoManagedObjectContext: managedObjectContext)
code?.the_code = "ali"
managedObjectContext?.save(nil)
您的核心數據代碼在哪裏?這非常簡單 - 一旦找到QR碼,您應該查詢現有代碼的核心數據。如果它不存在,請在您的「實體」中插入。如果確實存在,則抓取該記錄並更新它。 – LyricalPanda 2015-04-02 17:23:30
即時添加核心數據代碼 – 2015-04-02 17:27:38