Skip to content

Instantly share code, notes, and snippets.

View onevcat's full-sized avatar
🐭

Wei Wang onevcat

🐭
View GitHub Profile
public struct ContiguousArray<Element>: _DestructorSafeContainer {
internal typealias _Buffer = _ContiguousArrayBuffer<Element>
internal var _buffer: _Buffer
internal init(_buffer: _Buffer) {
self._buffer = _buffer
}
}
extension ContiguousArray: RandomAccessCollection, MutableCollection {
@onevcat
onevcat / Delegate.swift
Last active December 23, 2022 07:21
An auto-weak delegate for handle modern delegate pattern.
import Foundation
/// A class that keeps a weakly reference for `self` when implementing `onXXX` behaviors.
/// Instead of remembering to keep `self` as weak in a stored closure:
///
/// ```swift
/// // MyClass.swift
/// var onDone: (() -> Void)?
/// func done() {
/// onDone?()
@onevcat
onevcat / dictionary.stencil
Created March 12, 2018 04:54
dictionary.stencil
{# 找到所有实现了 DictionaryConvertible 的类型 #}
{% for t in types.implementing.DictionaryConvertible %}
{# 为该类型 t 创建 extension #}
extension {{ t.name }} {
var value: [String: Any] {
return [
{# 对类型中的所有存储属性迭代 #}
{% for val in t.storedVariables %}
{% if val.isArray %} {# 如果变量是数组,map 其中的值进行嵌套 #}
"{{val.name}}": {{val.name}}.map { $0.value }
@onevcat
onevcat / enumset.1.stencil
Created March 12, 2018 04:54
enumset.1.stencil
{% for enum in types.implementing.EnumSet|enum %}
@onevcat
onevcat / enumset.stencil
Last active March 12, 2018 04:53
enumset.stencil
{% for enum in types.implementing.EnumSet|enum %}
extension {{ enum.name }} {
{% if not enum.hasAssociatedValues %}
static let all: [{{ enum.name }}] = [
{% for case in enum.cases %} .{{ case.name }}{% if not forloop.last %},{% endif %}
{% endfor %}]
{% endif %}
static let count: Int = {{ enum.cases.count }}
}
{% endfor %}
@onevcat
onevcat / COW.swift
Last active September 12, 2023 08:25
import Foundation
var a = 123
var cpa = a
class Miao {}
var c = Miao()
var cc = c
struct S {}
var s = S()
@onevcat
onevcat / .lldbinit
Last active August 14, 2017 02:17
My LLDB init
command script import "~/Library/Application Support/Realm/rlm_lldb.py" --allow-reload
command script import /usr/local/opt/chisel/libexec/fblldb.py
command alias -H "Print description in ObjC context" -h "Print in ObjC" -- cpo expression -l objc -O --
command alias -H "Print description in Swift context" -h "Print in Swift" -- spo expression -l swift -O --
command alias -H "Print value in ObjC context in hexadecimal" -h "Print in hex" -- cpx expression -f x -l objc --
settings set target.x86-disassembly-flavor intel
settings set target.skip-prologue false
@onevcat
onevcat / Controller.cs
Last active August 30, 2023 08:01
Final script for UniWebView "Working with Code" sample
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controller : MonoBehaviour {
UniWebView webView;
// Use this for initialization
void Start () {
var status: OSStatus
let readOrWrite = kAudioFileGlobalInfo_ReadableTypes
var propertySize: UInt32 = 0
status = AudioFileGetGlobalInfoSize(readOrWrite,
0,
nil,
&propertySize)
print(status)
print(propertySize)
@onevcat
onevcat / demo.swift
Created April 6, 2017 00:56
Method literal performance
import QuartzCore
var a = 1
func methodParams(file: String = #file, fun: String = #function, line: UInt = #line) -> Int {
a += 1
return a
}
func method() -> Int {
a += 1