Skip to Main Content
iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
book

iOS 编程基础:Swift、Xcode 和Cocoa 入门指南

by Matt Neuburg
January 2017
Intermediate to advanced content levelIntermediate to advanced
552 pages
9h 43m
Chinese
China Machine Press
Content preview from iOS 编程基础:Swift、Xcode 和Cocoa 入门指南
124
4
static let hostile = "go away"
}
现在,其他地方的代码可以获取到
Greeting.friendly
Greeting.hostile
的值。该
示例非常有代表性;不变的静态
/
类属性可以作为一种非常便捷且有效的方式为代码提
供命名空间下的常量。
与实例属性不同,静态属性可以通过对其他静态属性的引用进行实例化,这是因为静态
属性初始化器是延迟的(参见第
3
章):
struct Greeting {
static let friendly = "hello there"
static let hostile = "go away"
static let ambivalent = friendly + " but " + hostile
}
注意到上述代码中没有使用
self
。在静态
/
类代码中,
self
表示类型本身。即便在
self
会被隐式使用的场景下,我也倾向于显式使用它,不过这里却无法使用
self
,虽
然编译器不会报错(我认为这是个
Bug
)。为了表示
friendly
hostile
的状态
,我
可以使用类型名字,就像其他代码一样:
struct Greeting {
static let friendly = "hello there"
static let hostile = "go away"
static let ambivalent = Greeting.friendly + " but " + Greeting.hostile
}
另外,如果将 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

机器学习实战:基于Scikit-Learn、Keras 和TensorFlow (原书第2 版)

Aurélien Géron
Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Swift 人工智能实战:从基础理论到AI 驱动的应用程序开发

Mars Geldard, Jonathon Manning, Paris Buttfield-Addison, Tim Nugent

Publisher Resources

ISBN: 9787111556350