Skip to content

Instantly share code, notes, and snippets.

View calvingit's full-sized avatar
😈

Calvin calvingit

😈
View GitHub Profile
@IBDesignable
class GradientView: UIView {
// implement cgcolorgradient in the next section
@IBInspectable var startColor: UIColor? {
didSet { gradientLayer.colors = cgColorGradient }
}
@IBInspectable var endColor: UIColor? {
didSet { gradientLayer.colors = cgColorGradient }
// AppStorage 需要 iOS 14 才可以用
@propertyWrapper
public struct UserDefault<Value>: DynamicProperty {
@State private var value: Value
let key: String
let store: UserDefaults
public init(
// 抽象一个 Animator
struct Animator {
typealias Animations = () -> ()
typealias Completion = (Bool) -> ()
let perform: (@escaping Animations, Completion?) -> ()
}
extension UIView {

编译没有问题,在 install 到iPhone上时报错了,类似下面的情况,但是 Code 错误码可能不一样。

  • 402653103: 错把静态库加载成动态库(尤其是 framework 类型的静态库,小心不要 Embded & Sign)
  • 402653415: 免费app达到最大数目
  • 402652994: 设备装有相同bundle id的应用
  • 402653150: iOS设备要用xcode12来调试
  • 402653170: 测试设备运行内存不足,退出部分或者所有后台应用试试
  • 402620395: 用dis证书来dev调试
  • 402620375: 打包macOS版本低于11,需要更新macOS版本或者设置--generate-entitlement-der(调用codesign之前调用或者在xcode->OTHER_CODE_SIGN_FLAGS设置)
  • 402653179: 手机内存满了

如果在引入第三方库时,厂商没有特别说明是静态库还是动态库,要搞清楚"Target" - "General" - "Frameworks, Libraries, and Embedded Content"- "Embed"选项:

  • 静态 framework:选择 "Do Not Embed"
  • 动态 framework: 选择 "Embed & Sign"

识别方法就是使用file xxx.framework/xxx命令,输出内容里面大概有两种:

  • current ar archive random library: 静态库
  • dynamically linked shared library: 动态库

简单判断就是看是否有单词“dynamically”,有就是动态库。

@calvingit
calvingit / UITapGestureRecognizer+TappedLabel
Created November 4, 2023 11:40
可点击链接的 UILabel
// 来源: https://stackoverflow.com/a/65980444
extension UITapGestureRecognizer {
func didTapAttributedString(_ string: String, in label: UILabel) -> Bool {
guard let text = label.text else {
return false
}
import Foundation
import CoreImage
@objcMembers
class QRCodeGenerator: NSObject {
/// 生成二维码
/// - Parameters:
/// - string: 字符串
@calvingit
calvingit / TextInputLimitDelegate.swift
Created November 4, 2023 11:39
限制 UITextField 和 UITextView 的输入
extension String {
/// 计算字节数,中文算非ASCII字符,占2个字节
func lengthInBytes() -> Int {
var length = 0
for char in self {
if char.isASCII {
length += 1
} else {
/// 要让 UITableView 点击空白处隐藏 Cell 里面的 UITextField 弹出的键盘,
/// 需要特殊处理一下手势响应,否则添加了 tapGesture 后点击其他 Cell 会不想要 didSelectRowAtIndexPath 回调
class ViewController: UIViewController, UIGestureRecognizerDelegate {
func viewDidLoad() {
super.viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapAction(_:)))
tapGesture.delegate = self
@calvingit
calvingit / iOS 13 之后 获取 KeyWindow 的方法.swift
Created November 4, 2023 11:37
iOS 13 之后 获取 KeyWindow 的方法
extension UIApplication {
var firstKeyWindow: UIWindow? {
return UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.filter { $0.activationState == .foregroundActive }
.first?.windows
.first(where: \.isKeyWindow)
}
}