欢迎关注微信公众号「Swift 花园」

导致我们浪费时间的是我们的认知错误

你是否经历过这样的场景。邻居过来串门,喋喋不休,我们频频点头回应,哪怕当下已经心急如焚;同事开始闲聊琐碎,而我们沉浸其中,不曾意识到本该用来工作的时间正在一点一点溜走;从熟人那里收到微信,对方说:“我可以向你讨教点事情吗?”,我们爽快答应,因为抹不开面子拒绝别人?

斯多葛哲学家塞内卡(Seneca)曾经惊叹:哪怕最聪明的人,在保护自己的时间这件事上,都不免显得愚蠢。

没有人会把自己的钱财送给路人,但我们中的许多人却送出了我们的生命!我们为财产和金钱所累,却罕有意识我们的时间正遭受的浪费。时间本应是我们每个人都最应吝惜的东西。

两千多年后的今天,为什么我们还在允许时间这种宝贵的资源从我们的手中不停地溜走?

阅读全文 »

欢迎关注微信公众号「Swift 花园」

About the CSTA K-12 Computer Science Standards
关于 CSTA K-12 计算机科学标准

Computer science and the technologies it enables rest at the heart of our economy and the way we live our lives. To be well-educate citizens in a computing-intensive world and to be prepared for careers in the 21st century, our students must have a clear understanding of the principles and practices of computer science. The CSTA K–12 Computer Science Standards delineate a core set of learning objectives designed to provide the foundation for a complete computer science curriculum and its implementation at the K–12 level. To this end, the CSTA Standards:

计算机科学及其支持的技术是我们经济的核心,也是我们生活的方式。为了对计算机密集型世界中的公民进行良好的教育并为 21 世纪的职业做好准备,我们的学生必须对计算机科学的原理和实践有清晰的了解。 CSTA K-12 计算机科学标准描述了一组核心学习目标,旨在为完整的计算机科学课程及其在 K-12 级别的实施提供基础。为此,CSTA 标准:

・Introduce the fundamental concepts of computer science to all students, beginning at the elementary school level.
・Present computer science at the secondary school level in a way that can fulfill a computer science, math, or science graduation credit.
・Encourage schools to offer additional secondary-level computer science courses that will allow interested students to study facets of computer science in more depth and prepare them for entry into the work force or college.
・Increase the availability of rigorous computer science for all students, especially those who are members of underrepresented groups.
The standards have been written by educators to be coherent and comprehensible to teachers, administrators, and policy makers.
Levels 1A, 1B, 2, and 3A are the computer science standards for ALL students. The Level 3B standards are intended for students who wish to pursue the study of computer science in high school beyond what is required for all students (specialty or elective courses).

  • 本文的主要价值: 提供一份软件开发中分治 UI 逻辑的实践样本
  • 关键词: 分治、 UOP

欢迎关注微信公众号「Swift 花园」

引子

我们知道,分治策略是人们解决问题的一种基本策略。问题规模越大,内部包含的差异化的细节越多,越需要执行分治策略。

所谓化整为零,化繁为简,逐个击破讲的都是分治。在计算机领域,我们要列举分治的例子,大的可以聊到七层网络模型(本质上分层也是一种分治),小的可以讲起二分算法。

本文基于 Android 客户端开发中经常涉及的交互逻辑编程展开,表达我对于 UI 分治在软件开发中如何实践的理解。

阅读全文 »

欢迎关注微信公众号「Swift 花园」

处理缺失的数据

我们已经会使用 Int 这样的类型来存储像 5 这样的数值。不过,当你想要存储用户年龄这样的属性,并且你还不知道该用户的年龄时你该怎么做呢?

你可能会说,“我可以暂时存成 0”,但这样一来你就会混淆新生儿和你不知道年龄的用户。你应该用一个特殊的数字,比如 1000 或者 -1 来代表 “未知”,这两个数字都不可能是年龄,但你能记得住这些特殊数字的含义吗?

Swift 的解决方案称为 optional ,即可选型。你可以基于任意类型创建可选型。一个可选型整数可以有诸如 1 或者 1000 这样的数字,也可能没有任何值,即值可以缺失, Swift 里表示缺失是用 nil

为了把一个类型变成可选型,只需要在类型后面加一个问号。举个例子,我们可以这样把一个整数变成可选型:

1
var age: Int? = nil
阅读全文 »

欢迎关注微信公众号「Swift 花园」

协议

协议是一种描述某个类型必须有某些属性和方法的方式。你告知 Swift 某个类型将使用某个协议,这个过程称为协议适配或者协议遵循。

举个例子,我们可以写一个函数接收 id 属性,但我们并不精确地关心用的是哪一种数据类型。让我们从 Identifiable 协议开始,这个协议要求所有遵循协议的类型必须有一个 id 字符串属性,并且这个字符串可读写。

1
2
3
protocol Identifiable {
var id: String { get set }
}

我们无法创建协议的实例,因为协议只是一种描述,它本身并非一种类型。但是我们可以创建遵循它的结构体。

1
2
3
struct User: Identifiable {
var id: String
}

最后,我们还可以写一个 displayID () 函数,它接收 Identifiable 对象:

1
2
3
func displayID(thing: Identifiable) {
print("My ID is \(thing.id)")
}
阅读全文 »