基本上我想要完成的是让 AVCaptureDevice 的前置摄像头成为 AVCaptureSession 期间应用程序上的第一个也是唯一的选项。
我环顾了 StackOverflow,提供的所有方法和答案都已从 iOS 10、Swift 3 和 Xcode 8 开始弃用。
我知道您应该使用 AVCaptureDeviceDiscoverySession 枚举设备并查看它们以区分正面和背面,但我不确定如何这样做。
有人可以帮忙吗?如果是这样就太棒了!
这是我的代码:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
previewLayer.frame = singleViewCameraSlot.bounds
self.singleViewCameraSlot.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
lazy var captureSession: AVCaptureSession = {
let capture = AVCaptureSession()
capture.sessionPreset = AVCaptureSessionPreset1920x1080
return capture
}()
lazy var previewLayer: AVCaptureVideoPreviewLayer = {
let preview = AVCaptureVideoPreviewLayer(session: self.captureSession)
preview?.videoGravity = AVLayerVideoGravityResizeAspect
preview?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
preview?.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
preview?.position = CGPoint(x: self.view.bounds.midX, y: …Run Code Online (Sandbox Code Playgroud) 预期行为:
对于 i = 0,打印语句在 0 秒后调用。
对于 i = 1,1.1 秒后调用打印语句
对于 i = 2,打印语句在 2.2 秒后调用
实际行为:
分别在 0、1、2、3 秒后调用打印语句,即忽略内部延迟函数。
那么为什么会出现差异呢?
for i in 0...3 {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(i), execute: {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(i/10), execute: {
print("function was called")
})
})
}
Run Code Online (Sandbox Code Playgroud) 我需要为简单的应用程序屏幕编写一些 UI 测试,该屏幕顶部有一个导航栏,屏幕其余部分有一个包含两个单元格的表格。
\n每个单元格包含:
\n这是一张图像,可让您了解屏幕上的元素\n
编写一个测试执行以下操作:
\nXCTest 无法识别包含错误的元素。
\n当我使用调试视图层次结构检查元素时,它就在那里,清晰可见,文本属性“不能少于或超过 10 位数字”,accessibiltyIndentifier“errorField”。
\n层次结构: UIWindow->UILayoutContainerView->UINavigationTransitionView->UIViewControllerWrapperView->UIView->Form->FormTextFieldCell->UITableViewCellContentView->UIView->UILabel
\n它有一个同级,也是 UILabel(“这是您必须放置 10 位会员卡的地方”) - 访问这个同级没有问题。我可以通过accessibilityIdentifier或其文本访问它:
\napp.staticTexts["hintField"]\napp.staticTexts["This is where you must put your 10-digit loyalty card"]\nRun Code Online (Sandbox Code Playgroud)\n但这些选项都不适用于我感兴趣的元素。当我打印 app.debugDescription 时,该元素不存在,就好像它不存在一样。
\n以下是用户输入错误并且错误文本已在屏幕上可见后 debugDescription 的输出:
\nElement subtree:\n \xe2\x86\x92Application 0x60000016e700: {{0.0, …Run Code Online (Sandbox Code Playgroud) 我有一系列元组(
var array = [("John Doe", 13), ("The Guy", 15), ("The Person", 19)]
Run Code Online (Sandbox Code Playgroud)
并希望按从最大到最小的int值对它们进行排序.最后,我希望数组看起来像这样
var array = [("The Person", 19), ("The Guy", 15), ("John Doe", 13)]
Run Code Online (Sandbox Code Playgroud)
我怎么做到这一点?
请帮帮我,我使用Xcode 8.3(swift 3.1),功能trimmingCharacters不起作用.我的代码如下:
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if var searchStr = textField.text{
let _searchStr = searchStr.trimmingCharacters(in: .whitespaces)
print("After trimming:\(_searchStr)")
}
}
Run Code Online (Sandbox Code Playgroud)
文本字段中的输入是409 hu?nh,打印结果409 hu?nh 不符合预期:409huỳnh.