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 入门指南
58
2
首先,匿名函数是降低代码的命令性,增强函数性的一种行之有效的方式:动作在需要
时才发生,而无须借助一系列的准备步骤。如下是个常见的
Cocoa
示例:创建并配置
一个
NSMutableParagraphStyle
,然后在对
addAttribute:value:range:
的调用中使用
content
是个
NSMutableAttributedString
)。
let para = NSMutableParagraphStyle()
para.headIndent = 10
para.firstLineHeadIndent = 10
// ... more configuration of para ...
content.addAttribute(
NSParagraphStyleAttributeName,
value:para, range:NSMakeRange(0,1))
我觉得上面的代码丑陋至极。我们只在
addAttribute:value:range
用中才需要将
para
作为
value:
参传递进去,因此在调用中创建并配置它才是更好的做法。
Swift
许我们这么做,我更倾向于下面这种写法:
content.addAttribute(
NSParagraphStyleAttributeName,
value: {
let para = NSMutableParagraphStyle()
para.headIndent = 10
para.firstLineHeadIndent ...
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