Get the accurate duration of a video

This works for me: import AVFoundation import CoreMedia … if let url = Bundle.main.url(forResource: “small”, withExtension: “mp4″) { let asset = AVAsset(url: url) let duration = asset.duration let durationTime = CMTimeGetSeconds(duration) print(durationTime) } For the video here it prints “5.568” which is correct. Edit from comments: A video that returns 707 seconds when divided by … Read more

How to set a default Tab in SwiftUIs TabView?

@State private var selection = 3 TabView(selection:$selection) { QuestionView() .tabItem { Image(systemName: “questionmark”) Text(“Questions”) } .tag(1) DataView() .tabItem { Image(systemName: “chart.bar.fill”) Text(“Data”) } .tag(2) Text(“Empty Tab right now”) .tabItem { Image(systemName: “bolt.horizontal.fill”) Text(“Trend”) } .tag(3) } In this case I set default selected the third Tab. Change selection to the desired Tab.

How to use a SwiftUI view in place of table view cell

Thanks for answering your own question here. Your solution helped me make a generic HostingTableViewCell class. I’ll post it here if anyone finds this question on Google like I did. import SwiftUI class HostingTableViewCell<Content: View>: UITableViewCell { private weak var controller: UIHostingController<Content>? func host(_ view: Content, parent: UIViewController) { if let controller = controller { … Read more

‘async’ call in a function that does not support concurrency

The answer here is that the “await getSomethingLater” must be called from an async context. Literally that means changing this: let result = await tryThis.getSomethingLater(3.141592653589793238462) print(“result: \(result)”) into this: Task { let result = await tryThis.getSomethingLater(3.141592653589793238462) print(“result: \(result)”) } So the whole thing becomes: class TryThis { func getSomethingLater(_ number: Double) async -> String { … Read more

Calling instance method on each object in array

What you are doing in items.forEach { $0.instanceMethod() } let mappedItems = items.map { $0.mappingInstanceMethod() } is a clean and Swifty way. As explained in Is there a way to reference instance function when calling SequenceType.forEach?, the first statement cannot be reduced to items.forEach(Item.instanceMethod) There is one exception though: It works with init methods which … Read more

How to edit the UIBlurEffect intensity?

You can do that in super elegant way with animator (reducing UIVisualEffectView alpha will not affect blur intensity, so we must use animator) Usage as simple as: let blurEffectView = BlurEffectView() view.addSubview(blurEffectView) BlurEffectView realisation: class BlurEffectView: UIVisualEffectView { var animator = UIViewPropertyAnimator(duration: 1, curve: .linear) override func didMoveToSuperview() { guard let superview = superview else … Read more

Swift – Custom setter on property

You can’t use set like that because when you call self.document = newValue you’re just calling the setter again; you’ve created an infinite loop. What you have to do instead is create a separate property to actually store the value in: private var _document: UIDocument? = nil var document: UIDocument? { get { return self._document … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)