SwiftUI——extension(扩展)

有时候swift需要使用的功能没有提供,就需要自己使用extension扩展。

扩展分为两种:对类型的扩展和对协议的扩展。

 

  • 对类型的扩展

比如说我们需要判断一个整数是不是偶数,swift并没有提供该功能的方法或者属性:

extension Int{
    var isEven: Bool{
        return self % 2 == 0
    }
}

   我们就是给Int类型扩展了一个计算属性,使用:

8.isEven

可以看到playground右侧返回的是true。这样我们就可以对所有的Int类型使用该属性。

 

  • 对协议的扩展

经常需要对数组和set使用一些功能,但是没有,就需要对其扩展。

由于数组和set都遵循Collection协议,我们就可以对Collection协议进行扩展,这样数组和set也可以使用扩展的功能或者属性。

比如说我们需要扩展一个功能,它能打印出所有的元素名称:

extension Collection{
    func printAll(){
        for name in self{
            print(name)
        }
    }
}

数组和set如下:

let arr = ["1","2","2","3","4","5"]
let se = Set(["小","中","大"])

使用:

arr.printAll()
se.printAll()

就可以看到playground下面打印出所有的元素。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In SwiftUI, you can use the `AVCaptureSession` and `AVCaptureVideoPreviewLayer` classes from the AVFoundation framework to record videos. Here's an example of how you can do it: ```swift import SwiftUI import AV struct ContentView: View { @State private var isRecording = false let captureSession = AVCaptureSession() let movieOutput = AVCaptureMovieFileOutput() var body: some View { VStack { if !isRecording { Button("Record") { startRecording() } } else { Button("Stop") { stopRecording() } } } .onAppear { setupCamera() } } func setupCamera() { let captureDevice = AVCaptureDevice.default(for: .video) guard let input = try? AVCaptureDeviceInput(device: captureDevice!) else { return } guard captureSession.canAddInput(input) else { return } captureSession.addInput(input) guard captureSession.canAddOutput(movieOutput) else { return } captureSession.sessionPreset = .high captureSession.addOutput(movieOutput) let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) previewLayer.videoGravity = .resizeAspectFill let rootLayer = NSView(frame: NSRect(x: 0, y: 0, width: 300, height: 300)) rootLayer.wantsLayer = true rootLayer.layer?.backgroundColor = NSColor.black.cgColor rootLayer.layer?.addSublayer(previewLayer) previewLayer.frame = rootLayer.bounds let uiView = NSHostingView(rootView: rootLayer) uiView.frame = CGRect(x: 0, y: 0, width: 300, height: 300) let viewController = NSViewController() viewController.view = uiView let window = NSWindow(contentViewController: viewController) window.makeKeyAndOrderFront(nil) captureSession.startRunning() } func startRecording() { let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask) let fileUrl = paths[0].appendingPathComponent("output.mov") movieOutput.startRecording(to: fileUrl, recordingDelegate: self) isRecording = true } func stopRecording() { movieOutput.stopRecording() isRecording = false } } extension ContentView: AVCaptureFileOutputRecordingDelegate { func fileOutput(_ output: AVCaptureFileOutput, didStartRecordingTo fileURL: URL, from connections: [AVCaptureConnection]) { print("Started recording to \(fileURL)") } func fileOutput(_ output: AVCaptureFileOutput, didFinishRecordingTo outputFileURL: URL, from connections: [AVCaptureConnection], error: Error?) { print("Finished recording to \(outputFileURL)") } } ``` This example sets up a camera preview using `AVCaptureVideoPreviewLayer` and records a video when the "Record" button is tapped. The recorded video is saved to the document directory with the name "output.mov". The recording can be stopped by tapping the "Stop" button. Please note that this code is for macOS development using SwiftUI. If you want to record videos in SwiftUI for iOS, you will need to make some modifications.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值