Skip to content

Instantly share code, notes, and snippets.

View quick_actions.swift
func sceneWillResignActive(_ scene: UIScene) {
var shortcutItems = UIApplication.shared.shortcutItems ?? []
if shortcutItems.isEmpty {
shortcutItems += [
UIApplicationShortcutItem(type: "Test Type 0", localizedTitle: "Test Title 0"),
UIApplicationShortcutItem(type: "Test Type 1", localizedTitle: "Test Title 1")
]
} else {
if let mutableShortcutItem = shortcutItems.first?.mutableCopy() as? UIMutableApplicationShortcutItem {
mutableShortcutItem.type = "Updated Type 0"
View quick_action_tapped.swift
func windowScene(_ windowScene: UIWindowScene, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
let alertController = UIAlertController(title: "Alert", message: "performActionFor \(shortcutItem.type)", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .cancel, handler: nil))
window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
View quick_action_launch.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
if let shortcutItem = connectionOptions.shortcutItem {
View context_menu_1.swift
struct ContentView: View {
var body: some View {
Text("Hello, World!")
.contextMenu {
Button(action: {
// copy the content to the paste board
}) {
Text("Copy")
Image(systemName: "doc.on.doc")
}
View context_menu_tableView.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let tableView = UITableView()
let tasks = [
Task(id: "S_1001", name: "Laundry", description: "Wash all the clothes.", tagColor: .blue),
Task(id: "S_1002", name: "Pick up kids", description: "Pick up the kids from the school.", tagColor: .red),
Task(id: "S_1003", name: "Walk the dog", description: "Walk the dog in the morning", tagColor: .green),
Task(id: "S_1004", name: "Yoga class", description: "The yoga class begins at 5 pm.", tagColor: .purple)
]
View context_menu_tableView_delegate.swift
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let task = tasks[indexPath.row]
return UIContextMenuConfiguration(identifier: task.id as NSString, previewProvider: nil) { _ in
let shareAction = UIAction(
title: "Share",
image: UIImage(systemName: "square.and.arrow.up")) { _ in
// share the task
}
let copyAction = UIAction(
title: "Copy",
View context_menu_tableView_delegate_submenu.swift
func tableView(_ tableView: UITableView, contextMenuConfigurationForRowAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let task = tasks[indexPath.row]
return UIContextMenuConfiguration(identifier: task.id as NSString, previewProvider: nil) { _ in
let blueAction = UIAction(title: "Blue") { _ in }
let redAction = UIAction(title: "Red") { _ in }
let greenAction = UIAction(title: "Green") { _ in }
let purpleAction = UIAction(title: "Purple") { _ in }
let tagActions = UIMenu(title: "Change Tag Color...", children: [blueAction, redAction, greenAction, purpleAction])
let shareAction = UIAction(
title: "Share",
View context_menu_preview_vc.swift
class TaskPreviewController: UIViewController {
let label = UILabel()
var task: Task!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
label.numberOfLines = 0
label.font = UIFont.systemFont(ofSize: 30)
view.addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
View context_menu_preview_show.swift
func tableView(_ tableView: UITableView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) {
guard let identifier = (configuration.identifier as? NSString) as String? else { return }
let task = tasks.first { $0.id == identifier }
if task != nil {
animator.addCompletion {
let previewController = TaskPreviewController()
previewController.task = task
self.show(previewController, sender: self)
}
}
View python_datetime_1.py
from datetime import datetime
datetime(2020, 1, 10, 13, 2, 0)
# a datetime object by specifying year, month, day, hour, minute, second
datetime.today()
# a datetime object with current date and time
datetime.now()
# the same as above when tz is None
datetime.utcnow()
# the same as above, but in the UTC (Coordinated Universal Time)