我有这个代码,可以用来从唱片黑胶唱片中读取条形码,然后,我尝试根据称为Discogs的网站上条形码的结果来获取信息。我已经在此网站上创建了一个应用程序,但这是必需的,但是每次阅读完条形码后,我的应用程序都会崩溃。 我可以读取条形码(获取其中的数字),然后将其viewcontroller退回到mainVC,在那里应显示结果,但我的应用在崩溃之前崩溃了... 我该怎么办 ?我正在使用alamofire
func setupCamera(){
session = AVCaptureSession()
let videoCaptureDevice = AVCaptureDevice.default(for: AVMediaType.video)
let videoInput : AVCaptureDeviceInput!
do {
videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice!)
} catch {
return
}
if (session.canAddInput(videoInput)){
session.addInput(videoInput)
} else {
scanningNotPossible()
}
// Create output object.
let metadataOutput = AVCaptureMetadataOutput()
// Add output to the session.
if (session.canAddOutput(metadataOutput)) {
session.addOutput(metadataOutput)
// Send captured data to the delegate object via a serial queue.
metadataOutput.setMetadataObjectsDelegate(self, queue: .main)
// Set barcode type for which to scan: EAN-13.
metadataOutput.metadataObjectTypes = [AVMetadataObject.ObjectType.ean13]
} else {
scanningNotPossible()
}
previewLayer = AVCaptureVideoPreviewLayer(session: session);
previewLayer.frame = view.layer.bounds;
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill;
view.layer.addSublayer(previewLayer);
// Begin the capture session.
session.startRunning()
}
func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) {
session.stopRunning()
if let metadataObject = metadataObjects.first {
guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return }
guard let stringValue = readableObject.stringValue else { return }
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
found(code: stringValue)
}
// dismiss(animated: true)
}
func barcodeDetected(code: String) {
// Let the user know we've found something.
let alert = UIAlertController(title: "Found a Barcode!", message: code, preferredStyle: UIAlertController.Style.alert)
let theAction = UIAlertAction(title: "Search", style: .default){ (action: UIAlertAction!) in
// Remove the spaces.
let trimmedCode = code.trimmingCharacters(in: .whitespaces)
// EAN or UPC?
// Check for added "0" at beginning of code.
let trimmedCodeString = "\(trimmedCode)"
var trimmedCodeNoZero: String
if trimmedCodeString.hasPrefix("0") && trimmedCodeString.count > 1 {
trimmedCodeNoZero = String(trimmedCodeString.dropFirst())
// Send the doctored UPC to DataService.searchAPI()
DataService.searchAPI(codeNumber: trimmedCodeNoZero)
} else {
// Send the doctored EAN to DataService.searchAPI()
DataService.searchAPI(codeNumber: trimmedCodeString)
}
print("popopop")
self.navigationController?.popViewController(animated: true)
}
alert.addAction(theAction)
self.present(alert, animated: true, completion: nil)
}
import Foundation
import Alamofire
import SwiftyJSON
class DataService {
static let dataService = DataService()
private(set) var ALBUM_FROM_DISCOGS = ""
private(set) var YEAR_FROM_DISCOGS = ""
static func searchAPI(codeNumber: String) {
// The URL we will use to get out album data from Discogs
let discogsURL = "\(DISCOGS_AUTH_URL)\(codeNumber)&?barcode&key=\(DISCOGS_KEY)&secret=\(DISCOGS_SECRET)"
Alamofire.request(discogsURL)
.responseJSON { response in
var json = JSON(response.result.value!)
let albumArtistTitle = "\(json["results"][0]["title"])"
let albumYear = "\(json["results"][0]["year"])"
self.dataService.ALBUM_FROM_DISCOGS = albumArtistTitle
self.dataService.YEAR_FROM_DISCOGS = albumYear
// Post a notification to let AlbumDetailsViewController know we have some data.
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "AlbumNotification"), object: nil)
}
}
}
日志错误:
> 2019-04-26 17:35:21.791189-0300 Discogs Barcode Example[1852:598955] -[Discogs_Barcode_Example.AlbumDetaisViewController setLabels]: unrecognized selector sent to instance 0x10230a460
2019-04-26 17:35:21.793519-0300 Discogs Barcode Example[1852:598955] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Discogs_Barcode_Example.AlbumDetaisViewController setLabels]: unrecognized selector sent to instance 0x10230a460'
*** First throw call stack:
(0x2016c3518 0x20089e9f8 0x2015e0278 0x22d83bef8 0x2016c8d60 0x2016ca9fc 0x2016345bc 0x201634588 0x201633a7c 0x201633728 0x2015ad524 0x2016331d8 0x20201b814 0x100f1cb38 0x101358d78 0x101325708 0x10209b6f0 0x10209cc74 0x1020aa6fc 0x201654ec0 0x20164fdf8 0x20164f354 0x20384f79c 0x22d810b68 0x100f1b03c 0x2011158e0)
libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:0)
[Discogs_Barcode_Example.AlbumDetaisViewController setLabels]:
似乎AlbumDetaisViewController
已挂接到名为setLabels
的操作方法上,并且您将其更改为其他名称,因此要么返回原始名称,要么清除IB的连接并使用新名称进行连接