Skip to content

Instantly share code, notes, and snippets.

View 623637646's full-sized avatar
🎯
Focusing

Yanni Wang 623637646

🎯
Focusing
View GitHub Profile
@623637646
623637646 / Dictionary+Extension.swift
Last active May 7, 2024 08:29
How to update a value in a nested dictionary given path fragment in Swift?
// Refer to https://stackoverflow.com/a/41543070/9315497
// https://talk.objc.io/episodes/S01E31-mutating-untyped-dictionaries
extension Dictionary {
subscript(jsonDict key: Key) -> [String: Any]? {
get {
return self[key] as? [String: Any]
}
set {
self[key] = newValue as? Value
@623637646
623637646 / Swift VS Objective-C.txt
Last active December 13, 2023 09:14
Swift VS Objective-C
swift vs Objective-C
`?` 可选值 显式可选 vs 隐式可选
异步编程方式 async 协程编程 vs OKBAsyncLoader
enum 枚举 可携带变量 vs 不可携带变量
函数&方法抛异常 显式抛异常 vs 隐式抛异常
并发 Task结构化并发 vs 非结构化并发
模型序列化反序列化 原生支持Codable vs 第三方库支持
泛型 强大的泛型 vs 鸡肋的泛型
响应式编程,观察者模式 原生Combine vs 第三方库 RxSwift, RecativeCocoa
@623637646
623637646 / SwiftUIBug.swift
Created August 19, 2023 06:16
SwiftUI bug: Refresh logic of List is invalid in this case.
import SwiftUI
/// This demo shows a bug of SwiftUI. It's related to the refresh logic of "List"
///
/// The steps are:
/// 1. click "Go to next page"
/// 2. click "Click Me"
/// 3. Go back to home page
@623637646
623637646 / RoundPixelByScale.m
Created June 18, 2021 10:47
Round Pixel By Scale for iOS
CGFloat YNSelfSizingRoundPixelValue(CGFloat value)
{
static CGFloat scale;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^(){
scale = [UIScreen mainScreen].scale;
});
return roundf(value * scale) / scale;
}
@623637646
623637646 / 插桩
Created September 19, 2020 15:15
插桩
插桩技术
插桩技术是指将额外的代码注入程序中以收集运行时的信息,可分为两种:
(1)源代码插桩[Source Code Instrumentation(SCI)]:额外代码注入到程序源代码中。
(2)二进制插桩(Binary Instrumentation):额外代码注入到二进制可执行文件中。
@623637646
623637646 / download.py
Created July 4, 2020 08:00
xiuren image spider
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Created on 2020-03-12 15:56:36
# Project: Girls
from pyspider.libs.base_handler import *
import os
import sys
@623637646
623637646 / Swift内存规律
Created May 7, 2020 06:24
Swift中,一个类实例的内存布局是有规律的.
Swift中,一个类实例的内存布局是有规律的:
1. 32位机器上,类前面有4+8个字节存储meta信息,64位机器上,有8+8个字节;
2. 内存中,字段从前往后有序排列;
3. 如果该类继承自某一个类,那么父类的字段在前;
4. Optional会增加一个字节来存储.None/.Some信息;
5. 每个字段需要考虑内存对齐;
这方面尚未从官方的资料找到参考,上述规律一些是从网上其他大神的总结中收集,一些从Clang的一些说明文档中挖掘,加上自己的反复验证得到。
Refer to: https://www.jianshu.com/p/eac4a92b44ef
@623637646
623637646 / InterestTests.swift
Last active April 25, 2020 14:06
Calculate interest
//
// InterestTests.swift
// iOSHookTests
//
// Created by Yanni Wang on 25/4/20.
// Copyright © 2020 Yanni. All rights reserved.
//
import XCTest
static CGFloat MDCFabs(CGFloat value) {
#if CGFLOAT_IS_DOUBLE
return fabs(value);
#else
return fabsf(value);
#endif
}
static BOOL MDCCGFloatEqual(CGFloat a, CGFloat b) {
const CGFloat constantK = 3;
@623637646
623637646 / AutoreleasingUnsafeMutablePointer.swift
Last active November 21, 2019 08:24
A bug of AutoreleasingUnsafeMutablePointer compiling
import UIKit
class ViewController: UIViewController {
struct OneStruct {
}
class OneClass {
}