Skip to content

Instantly share code, notes, and snippets.

//
// HyperlinkLabel.swift
// SLHome
//
// Created by yongren on 2022/1/6.
//
import Foundation
/// A UILabel subclass that responds to links tap.
@13hoop
13hoop / Server.swift
Created September 24, 2021 07:00
this is a mock API server demo
class Server {
private init() {}
static let shared = Server.init()
private static let serverQueue = DispatchQueue.global(qos: .background)
func getCountries(completion: @escaping (Array<String>) -> Void) {
let countries = ["France", "Germany", "Spain", "Portugal"]
let delay = Int.random(in: 1..<4)
Self.serverQueue.asyncAfter(deadline: .now() + .seconds(delay)) {
completion(countries)
}
@13hoop
13hoop / libdispatch-efficiency-tips.md
Created February 26, 2020 07:37 — forked from tclementdev/libdispatch-efficiency-tips.md
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

I suspect most developers are using the libdispatch inefficiently due to the way it was presented to us at the time it was introduced and for many years after that, and due to the confusing documentation and API. I realized this after reading the 'concurrency' discussion on the swift-evolution mailing-list, in particular the messages from Pierre Habouzit (who is the libdispatch maintainer at Apple) are quite enlightening (and you can also find many tweets from him on the subject).

My take-aways are:

/**-------- swift 集合类型解读 之 Collection - 1213/17
通过定一个 队列 来熟悉 Collection协议
其区别于 栈 ,栈的实现可以通过数组Array实现,但其会在练习内存中持有元素,这对 pop & push 来说很简单,但一旦有 remove(at: ) 这样的操作,时间复杂就会是 O(n)
*/
protocol YRQueen {
associatedtype Element
// 入队
mutating func enQueen(_ newElement: Element)
package net.jmecn.math;
/**
* 四元数
*
*
*/
public class Quaternion {
public float x, y, z, w;
@13hoop
13hoop / SequenceDemo1212.swift
Last active December 12, 2018 01:54
补充1211的东西,除了Sequence本身之外,还要注意一下 代码组合 的方式,并非继承, 而是通过 protocol
//-------- swift 集合类型解读 之 Sequence - 1212
/*
另一个 完备的模拟使用,比如在 终端上的打印所有的输入过程
*/
// a 实现一个 Iterator
struct PrefixIterator: IteratorProtocol {
let str: String
var offset: String.Index
//-------- swift 集合类型解读 - 1210
/*
--> Sequence 协议
这个协议是内置集合类型的核心,正是它让 针对元素序列的通用操作 成为可能
其规定了要实现跟返回一个 迭代器(Iterator)
protocol Sequence {
associatedtype Iterator: IteratorProtocol // 这里使用了 关联类型,作用就是协议中的 “范型” 或者 “alias”
func makeIterator() -> Iterator
}
--> IteratorProtocol 协议,一般只有在自定义序列类型的时候才使用
@13hoop
13hoop / protocol001.swift
Created December 6, 2018 01:51
协议 的实现效果是可以被延续的,本质上这是一种多态,而不是继承 ,故其不可以被重写,因为override的只能是 父类 的 methord
protocol SoundsFunc {
func voice () -> String
}
class Computer {
var name: String
init(name: String) {
self.name = name;
}
func calculat() {
@13hoop
13hoop / goroutines.swift
Created February 18, 2018 15:37 — forked from chriseidhof/goroutines.swift
goroutines.swift
import Foundation
protocol Channel: IteratorProtocol {
func send(_ value: Element?)
}
/// A blocking channel for sending values.
///
/// `send` and `receive` must run in separate separate execution contexts, otherwise you get a deadlock.
final class BlockingChannel<A>: Channel {
@13hoop
13hoop / .js
Last active May 22, 2017 09:13
export function currentUser() {
let user = AV.User.current()
if (user) {
// let r = parseUserFromAVUser(user)
// console.log( '-- user --: ' + r['objectId'])
return parseUserFromAVUser(user)
} else {
return undefined
}
}