官术网_书友最值得收藏!

Foundation data types

When using foundation data types, Swift provides an overlay that makes interacting with them feel like they are native Swift types. We use this overlay to interact with foundation types, such as CGSize and CGRect for iOS applications (NSSize and NSRect for OS X applications). When developing iOS or OS X applications, we will interact with foundation types on a regular basis, so it is good to see this overlay in action.

Let's look at how to initialize some foundation data types. The following example defines NSRange, CGRect, and NSSize:

var range = NSRange(location: 3, length: 5)
var rect = CGRect(x: 10, y: 10, width: 20, height: 20)
var size = NSSize(width: 20, height: 40)

The overlay also lets us access the properties and functions in a way that feels like native Swift types. The following example shows us how to access the properties and functions:

var rect = CGRect(x: 10, y: 10, width: 20, height: 20)
rect.origin.x = 20 
//Changes the X value from 10 to 20

var rectMaxY = rect.maxY
//rectMaxY contains 30 (value of y + value of height)

var validRect = rect.isEmpty
//validRect contains false because rect is valid.

In the preceding code, we initialize a CGRect type. We then change the property of x from 10 to 20, retrieve the value of the maxY property, and check the isEmpty property to see whether we have a valid CGRect type.

We just barely scratched the surface of the interoperability between Swift and Objective-C. We will discuss this interoperability in depth in Chapter 11, Using Mix and Match later in this book.

主站蜘蛛池模板: 沂南县| 买车| 鹰潭市| 会同县| 井冈山市| 石家庄市| 天水市| 乌鲁木齐市| 辽中县| 诸暨市| 八宿县| 左云县| 伊宁县| 大洼县| 新宁县| 固安县| 连城县| 贞丰县| 垣曲县| 博兴县| 高雄县| 赤壁市| 库尔勒市| 应用必备| 独山县| 虹口区| 东城区| 谢通门县| 石门县| 贵州省| 会泽县| 黄龙县| 蓝田县| 富宁县| 巴东县| 凤翔县| 怀安县| 天峻县| 嘉鱼县| 扶余县| 迭部县|