gpt4 book ai didi

ios - Swift 中的 CGPointMakeWithDictionaryRepresentation

转载 作者:行者123 更新时间:2023-11-29 02:38:06 24 4
gpt4 key购买 nike

我正在使用 AVCaptureSession在 Swift 应用程序中扫描二维码。我想在检测到的 QR 码周围画一个框,但我在转换 AVMetadataMachineReadableCodeObject 的“角”属性时遇到问题变成可用的东西。

var corners: [AnyObject]! { get }

The value of this property is an array of CFDictionary objects, each of which has been created from a CGPoint struct using the CGPointCreateDictionaryRepresentation function, representing the coordinates of the corners of the object with respect to the image in which it resides.

我试过这个(基于 project by werner77 )但我得到以下编译器错误 “'CGPoint?'与‘CGPoint’不同”

// MARK: - AVCaptureMetadataOutputObjectsDelegate
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {

let metadataObject = metadataObjects[0] as AVMetadataMachineReadableCodeObject;
var corners = metadataObject.corners as Array<NSDictionary>;
var topLeftDict = corners[0] as NSDictionary;
var topLeft : CGPoint?

// COMPILE ERROR: 'CGPoint?' is not identical to 'CGPoint'
CGPointMakeWithDictionaryRepresentation(topLeftDict, &topLeft)
}

如有任何帮助,我们将不胜感激。

最佳答案

了解可选项

任何类型都可以附加?,这意味着它也可以是nil。这样做的好处是,您必须显式地处理或忽略 nil 对象,这与 Objective-C 不同,在 Objective-C 中,nil 对象可能会导致无法追踪的错误。

当从字典中获取对象时,它必须是可选的,因为字典中可能不存在该键。 CGPointMakeWithDictionaryRepresentation 需要一个非可选值,因此您必须使用初始化的非可选值

// Playground:
var point = CGPointMake(1, 2)
var dictionary = CGPointCreateDictionaryRepresentation(point)
var aPoint = CGPointZero
CGPointMakeWithDictionaryRepresentation(dictionary, &aPoint)
aPoint // x 1, y 2

关于ios - Swift 中的 CGPointMakeWithDictionaryRepresentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26124371/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com