Skip to content

Instantly share code, notes, and snippets.

View filsv's full-sized avatar
🏠
Working from home

Fil Sviatoslav - Stand with Ukraine filsv

🏠
Working from home
View GitHub Profile
// 'App' is ambiguous for type lookup in this context - Error and how to fix it:
import SwiftUI
import RealmSwift
// MARK: - Issue
@main
struct MyApp: App { // <- 'App' is ambiguous for type lookup in this context
init() {
// Configure Realm
@filsv
filsv / WatchSessionManager.swift
Last active April 8, 2023 21:33
WatchConnectivity Singleton (Swift 5+, iOS+WatchOS Targets) + How to (comment below)
import WatchConnectivity
class WatchSessionManager: NSObject, WCSessionDelegate {
static let sharedManager = WatchSessionManager()
private override init() {
super.init()
}
private let session: WCSession? = WCSession.isSupported() ? WCSession.default : nil
@filsv
filsv / VideoThumbnailHelper.swift
Last active September 20, 2019 16:35
Image thumbnail from video
import AVFoundation
func getThumbnail(url: URL) -> UIImage? {
do {
let asset = AVURLAsset(url: url , options: nil)
let imgGenerator = AVAssetImageGenerator(asset: asset)
imgGenerator.appliesPreferredTrackTransform = true
let cgImage = try imgGenerator.copyCGImage(at: CMTimeMake(0, 1), actualTime: nil)
let thumbnail = UIImage(cgImage: cgImage)
@filsv
filsv / DegreesRadiansHelper.swift
Last active May 28, 2018 19:55
Degrees <=> Radians
class DegreesRadiansHelper {
static func deg2Rad(_ degrees: Double) -> Double {
return degrees * .pi / 180
}
static func rad2Deg(_ radians: Double) -> Double {
return radians * 180.0 / .pi
}
}
@filsv
filsv / DelayHelper.swift
Created May 28, 2018 18:58
Cancelable Delay Function - Swift 4.0
class DelayHelper {
private var cancelled = false
static func run(delay: Double, closure: @escaping () -> ()) {
DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
if !self.cancelled {
closure()
}
}
}
@filsv
filsv / UIViewExtension+RoundCorners.swift
Last active March 16, 2017 10:21
Apply round corner/s on UIView, you can apply round corner to any side of UIView
func roundCorners(corners: UIRectCorner, radius: CGFloat)
{
let size = CGSize(width: radius, height: radius)
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: size)
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
@filsv
filsv / CALayer+Border.swift
Created December 26, 2016 13:40
Swift 3 Extension for Adding Border on one of the sides (or for all sides).
// Layer extension for adding border on one of the sides
extension CALayer {
func addBorder(edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
let border = CALayer()
switch edge {
case UIRectEdge.top:
border.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: thickness)
@filsv
filsv / CellReordering - UITableView.swift
Last active December 26, 2016 10:02
UITableViewCell Reordering
var arrays: [[String: AnyObject]] = [[String: AnyObject]]()
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let dic = self.arrays[sourceIndexPath.section]
if let array = dic["arrays"] as? NSMutableArray