Dev's Logging

Welcome to my blog !


Shall we always use [unowned self] inside closure in Swift

Published at November 21, 2019 ·  1 min read

在什么情况下使用 unowned self 和 weak self,weak 是一个 Optional 类型,意味着,需要判断其值是否为 nil,而 unowned 如果值为 nil 则会 crash 整个程序。”The difference between unowned and weak is that weak is declared as an Optional while unowned is not. By declaring it weak you get to handle the case that it might be nil inside the closure at some point. If you try to access an unowned variable that happens to be nil, it will crash the whole program....

Setting And Getting Data With UserDefaults In Swift

Published at November 21, 2019 ·  1 min read

UserDefaults 与浏览器中的 localstorage 颇为相似,可以方便用户存储有些相关配置,但数据超过 100k 时,则不合适,这将会减慢应用启动的速度。 支持的数据类型有 Bool, Float, Double, Int, String, URL,以及更为复杂的 Array, Dictionary, Date 和 Data 类型。其数据存储在后缀为 .plist 的文件中,一般为 Info.plist,当然其中也包含了应用名称和 Bundle ID 等数据。 存储时需要 UserDefaults.standard.set(“Arthur Dent”, forKey: “name”),传 key 和 value 即可,但获取则需要指定相应的数据类型,如: 支持的有如下几种: url(forKey:), array(forKey:), dictionary(forKey:), string(forKey:), stringArray(forKey:) (with type [String]?), data(forKey:), bool(forKey:), integer(forKey:), float(forKey:), and double(forKey:) ``` Reading and writing basics: UserDefaults Setting And Getting Data With UserDefaults In Swift...

Gestures in SwiftUI

Published at November 19, 2019 ·  1 min read

学习 iOS 开发遇到最大的阻力来自于,其拥有太多本身系统所拥有的特性,除了 Swift 本身的各种语法糖外,还有各种衍生出的内置酷,在这个过程中,不得不大量查阅开发文档即相关教程以了解其用法。 Gesture 便是其中一例。SwiftUI 除了提供 Gesture 相关的几个函数,TapGesture, DragGesture, RotationGesture, MagnificationGesture, LongPressGesture 外,还有 @GestureState 属性,与 Gesture 绑定监听状态。 Gestures in SwiftUI...

Weak self and unowned self explained in Swift

Published at November 19, 2019 ·  1 min read

由于 ARC(Automatic Reference Counting)的机制,未声明 weak 的对象均为“强引用“,也就是当相关对象赋值为 nil 时,其并未在内存中销毁。 也就是在下述例子中, blog 和 blogger 均赋值为 nil 时, deinit 函数并未被触发,因为存在 “var owner: Blogger?“ 和 “var blog: Blog?“ 默认的强引用,当将其设为 weak 时 “weak var owner: Blogger?“, 则会触发 deinit 函数。 class Blog { let name: String let url: URL var owner: Blogger? init(name: String, url: URL) { self.name = name; self.url = url } deinit { print("Blog \(name) is being deinitialized") } } class Blogger { let name: String var blog: Blog?...

Overriding methods in Swift extensions

Published at November 15, 2019 ·  1 min read

Extensions 作为 Swift 中非常常见的一种语法,与常见的 Class 却有些异同,虽然同样可以继承,但应用更多的是对现有 class “添加“一些新的功能,应注意的是,并不能对原有的函数进行 override。 Extensions cannot/should not override. It is not possible to override functionality (like properties or methods) in extensions as documented in Apple's Swift Guide. Extensions can add new functionality to a type, but they cannot override existing functionality. Apple Developer Guide 下述代码是一个很好的例子,作为 extension,可以创建和 class 同样的函数名,但必须保证参数名不一样,这样达到对源类“添加“新功能的目的,若参数名相同时,则会出现 “invalid redeclaration“ 的错误。 class Airplane { var altitude: Double = 0 func setAltitude(feet: Double) { print("feet", feet) altitude = feet } } extension Airplane { func setAltitude(meter: Double) { print("meter", meter) altitude = meter * 3....

Throttle vs Debounce in RxSwift

Published at November 15, 2019 ·  1 min read

Throttle 和 Debounce 在 RxSwift 中的区别。 Throttle:在某一时间段内最多触发一次 Debounce:当上一请求之后某一时间内没有任何后续操作后将其触发。 Throttle vs Debounce in RxSwift...

Thinking in RxSwift

Published at November 14, 2019 ·  1 min read

长期在 OOP 思维下进行编码,对 Reactive Programming 理解颇有难度,与 Function Programming 中 map filter 之类有些相似。 这篇文章对 RxSwift 进行了详细完整的介绍,值得细细咀嚼。 Thinking in RxSwift...

how to reclaim large space from .git, local cache of git

Published at November 14, 2019 ·  1 min read

在某生产环境中,可能由于程序运行存储在本地的大量未 tracked 的日志即生产相关文件,导致 .git 内的 objects 大量增常,多达 10 G。需要将其进行清理,以减少到合理的范围。 遇到的问题与此问题 How to shrink the .git folder 中的描述颇为相似,但测试过被接受答案和高赞答案后,均无果。 在此文章中How to Shrink a Git Repository ,“While we may have rewritten the history of the repository, those files still exist in there, stealing disk space and generally making a nuisance of themselves“ 当前项目中并没有 “.git/refs/original/” 文件夹,于是联想到,可能需要将本地 git cache 清除,“git rm –cached -r .“ 然后再运行上述文章中的命令 git reflog expire --expire=now --all git gc --prune=now git gc --aggressive --prune=now 然后,对分支进行修改,在生产环境中执行 ‘git pull’ 能够正常运行。...

Named return values in Go function

Published at November 7, 2019 ·  1 min read

在阅读 Clash 源码过程中,发现 clash/adapters/outbound/base.go 这个函数中的匿名函数中引用了一个返回值 ’t’,而未在函数中声明,这里涉及到的是 Golang 函数中关于被命名返回值的定义,如下所述,Go 中的命名返回值相当于在函数“最顶端“的定义的变量,优先级要高于一般的函数哪定义变量,因此,这个变量在函数中是需要像常规变量一样被使用,return 后无需有值。 Go's return values may be named. If so, they are treated as variables defined at the top of the function. Named return values...

Tags

abcs accept acid activemq affinity algorithm allocation android array async aws b+tree b-tree backoff benchmark best-practices bfs big-o bigquery bind bitcount blog break broker bubble buffer cache cap cert cgroups channel citus class classmethod closure closures cluster concurrency config consistency consumer container context cookie cors crawler cronjob csrf ctr data-science data-structure database datadog dataflow datascience decorator deepcopy defer dfs distributed django dns docker double-shipping drf ecosia elastic-search enumerate epoll equal errgroup escaping event extra fabric2 facebook-pixel financial-report flask flutter forward-proxy freelance frontend frp garbage-collector gc gcp generator gesture get gil git golang goroutine graphql ha handbook haproxy hash hash-slot hashring hashtable hpa http http-auth http-proxy http_proxy https index init innodb instagram intention-lock intergration interview ios is javascript jinja2 jobboard json jwt k8s kafka kibana kqueue label lambda layer4 layer7 lean levels.io linked-list linux list listen loadbalancer lock logs long-tail lru marketing master matplotlib memory merge metaclass metaprogramming metrics metrics-server microservices mitm mobile model mongo mongoose mq myisam mysql namespace nat netflix network network-extension nginx nodejs nomad nosql notification npm oodesign openssl optimization orm osi pandas parallelism paramiko parkinglot patroni permission pg pipeline pixelme post postgresql postresql prefetch_related prerender private-key process proxy proxycommand put pvm python queue rabbitmq rbac react-native reactive reactjs rebase redis redis-cluster replication resource rest restfulapi retargeting retry revenue reverse-proxy rocketmq rsa rxswift saas scaffold scaleable search-engine security select seo serverless service session set shadosocks shadowsocks shard sharding shell shopify sigint signal sigterm singleton slack slave slow-query sniper sns socket socks5 source-code spa sql sqlalchemy sqs ssh ssl ssl-pinning stack startup state stateful stateless staticmethod string struct swift swiftui switch syscall system-design systemctl tcp tcp-proxy thread tmpreaper token traefik trustkit tunning type typeform udp userdefaults variable vc voidcallback vpn vuejs weak web web-development where widget with yarn zset 削峰 单例模式 宽索引 异步 看源码学-golang 窄索引 解耦 跨域 跳板机


Archives

2020 (6)
2019 (157)
0001 (5)