Skip to content

Instantly share code, notes, and snippets.

View calvingit's full-sized avatar
🎯
Focusing

Wen calvingit

🎯
Focusing
View GitHub Profile
import UIKit
extension UIView {
/// UIView 添加 Auto Layout
@objc func layout(_ constraints: [NSLayoutConstraint]) {
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate(constraints)
}
/// 简化多次赋值的变量初始化
public protocol Then {}
extension Then {
@discardableResult
@inlinable
public func `do`(_ block: (Self) throws -> Void) rethrows -> Self {
try block(self)
return self
}
}

https 代理

~/.gitconfig 文件里配置

[http]
  proxy = socks5://127.0.0.1:1080
[https "github.com"]
  proxy = socks5://127.0.0.1:1080
 sslverify = false

1. UITableView 在编辑模式下可以实现默认的排序功能

    // Override to support rearranging the table view.
    override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
        tableView.moveRow(at: fromIndexPath, to: to)
    }
       
    // Override to support conditional rearranging of the table view.
    override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
extension UIGestureRecognizer {
private struct AssociatedKeys {
static var action = "action"
}
private typealias GestureAction = ((UIGestureRecognizer) -> Void)
private var action: GestureAction? {
get {

Popover

class ViewController: UIViewController {

    @IBAction func clicked(_ sender: Any) {

        let vc = UIHostingController(rootView: AlertView(cancel: {
            //
            self.dismiss(animated: true)
extension View {
func hideNavigationBar(_ hidden: Bool = true) -> some View {
if #available(iOS 16.0, *) {
return self.toolbar(hidden ? .hidden : .visible)
} else {
// Fallback on earlier versions
return self.navigationBarHidden(hidden)
}
}
}
import UIKit
// MARK: - Initializers
extension UIAlertController {
/// Create new alert view controller.
///
/// - Parameters:
/// - style: alert controller's style.
/// - title: alert controller's title.
import SwiftUI
import UIKit
/// 创建一个SwiftUI的容器View,将UIViewControler包含在内,即可预览
///
/// 如果要预览这个ViewController
///
/// ```swift
/// class ViewController: UIViewController {
extension String {
// 移除空白
func removingWhitespace() -> String {
let whitespacePattern = "\\s+"
let regex = try? NSRegularExpression(pattern: whitespacePattern, options: .caseInsensitive)
let range = NSRange(location: 0, length: count)
return regex?.stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "") ?? self
}