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 入门指南
84
3
精度)以及扩展精度的
Float80
;在
Core Graphics
框架中还有
CGFloat
(其大小可以是
Float
Double
,这取决于架构的位数)。
在使用
C API
时还会遇到
C
数值类型。对于
Swift
来说,这些类型只是类型别名而已,
这意味着它们是其他类型的别名;比如,
CDouble
(对应于
C
double
)只是
Double
的另一个名字,
CLong
C
中的
long
)是
Int
类型等。很多其他的数值类型别名都会出
现在各种
Cocoa
框架中;比如,
NSTimeInterval
只是
Double
的类型别名而已。
问题来了。我之前曾说过,不能通过变量赋值、传递或组合不同数值类型的值;你只能
显式将这些值转换为正确的类型才行。不过,现在你面对的是
Cocoa
中众多类型的数值!
Cocoa
传递给你的数值很可能既不是
Int
也不是
Double
,你可能根本就发现不了,直到
编译器告诉你出现了类型不匹配的情况。接下来,你需要搞清楚到底什么地方错了,然
后将这些变量转换为相同的类型。
如下这个典型示例来自于我的应用。我有一个
UIImage
,将其
CGImage
抽取出来,现在
想要通过
CGSize
来表示该
CGImage
的大小:
let mars = UIImage(named:"Mars")!
let marsCG = mars.CGImage
let szCG = CGSizeMake( // compile error
CGImageGetWidth(marsCG),
CGImageGetHeight(marsCG) ...
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