Skip to content

Instantly share code, notes, and snippets.

@saoudrizwan
saoudrizwan / os_log.swift
Last active October 5, 2019 05:11
Easy way to log file name, function, and line number to Console using Unified Logging in Swift. See String format specifiers: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html
import os.log
os_log("[%{public}@/%{public}@:%{public}@] This is an error message", log: OSLog(subsystem: "my.system", category: "Networking"), type: OSLogType.error, ("\(#file)" as NSString).lastPathComponent, "\(#function)", "\(#line)")
// Alternatively use a global helper method
enum LogCategory: String {
case `default` = "Default"
case networking = "Networking"
}
struct Message {
let id: String
let body: String
}
let messages = [Message(id: "1", body: "..."), Message(id: "2", body: "..."), Message(id: "3", body: "...")]
// or retrieve your messages from Disk...
let messages = try! Disk.retrieve("messages.json", from: .documents, as: [Message].self)
var messagesWithoutIdOf2 = [Message]()
@saoudrizwan
saoudrizwan / ForEach.swift
Last active November 16, 2017 09:41
Example of using .forEach()
let logs = ["Error", "Created", "Crashed"]
logs.forEach { print($0) }
// prints "Error"
// prints "Created"
// prints "Crashed"
let sameUrl = URL(string: "https://i.redd.it/dj4bz294zqhz.png")!
TaskManager.shared.dataTask(with: sameUrl) { (data, response, error) in
// ...
}
TaskManager.shared.dataTask(with: sameUrl) { (data, response, error) in
// ...
}
@saoudrizwan
saoudrizwan / TaskManager.swift
Last active May 10, 2024 10:56
Handle multiple network requests with the same URL efficiently with a shared task manager
import Foundation
class TaskManager {
static let shared = TaskManager()
let session = URLSession(configuration: .default)
typealias completionHandler = (Data?, URLResponse?, Error?) -> Void
var tasks = [URL: [completionHandler]]()
import UIKit
var feedbackGenerator : UISelectionFeedbackGenerator? = nil
@IBAction func gestureHandler(_ sender: UIPanGestureRecognizer) {
switch sender.state {
case .began:
// Instantiate a new generator
feedbackGenerator = UISelectionFeedbackGenerator()
import UIKit
class ResponsiveView: UIView {
override var canBecomeFirstResponder: Bool {
return true
}
}
class ViewController: UIViewController {
let saveMenuItem = UIMenuItem(title: "Save", action: #selector(saveTapped))
let deleteMenuItem = UIMenuItem(title: "Delete", action: #selector(deleteTapped))
UIMenuController.shared.menuItems = [saveMenuItem, deleteMenuItem]
@objc func saveTapped() {
// ...
}
@objc func deleteTapped() {
// ...
// Add our responsive view to a super view
let responsiveView = ResponsiveView(frame: CGRect(x: 100, y: 300, width: 50, height: 50))
self.view.addSubview(responsiveView)
// make responsiveView self.view's super window's first responder
responsiveView.becomeFirstResponder()
// Set up the shared UIMenuController
let menu = UIMenuController.shared
menu.arrowDirection = .default // up, down, left, right
import UIKit
class ResponsiveView: UIView {
override var canBecomeFirstResponder: Bool {
return true
}
}