- iOS Programming Cookbook
- Hossam Ghareeb
- 118字
- 2021-07-09 18:29:43
Adding new initializer
Extensions allow you to add new initializer to the currently available initializer for any particular type. For example, let's take a look at the CGRect class. CGRect has three initializers: empty init; init with origin and size; and init with x, y, width, and height. We will add new initializer with a center point and a rectangular size. Let's take a look at how to do it:
extension CGRect{ init(center:CGPoint, size:CGSize){ let x = center.x - size.width / 2 let y = center.y - size.height / 2 self.init(x: x, y: y, width: size.width, height: size.height) } } let rect = CGRect(center: CGPoint(x: 50, y: 50), size: CGSizeMake(100, 80)) // {x 0 y 10 w 100 h 80}
推薦閱讀
- Puppet實戰
- Learning Windows Server Containers
- 發布!設計與部署穩定的分布式系統(第2版)
- Persistence in PHP with the Doctrine ORM
- vSphere Virtual Machine Management
- SharePoint 2013 WCM Advanced Cookbook
- SharePoint 2013 應用開發實戰
- Linux操作系統應用編程
- Windows Server 2019 Administration Fundamentals
- 蘋果OS X Mavericks 10.9應用大全
- Linux基礎使用與案例
- Vim 8文本處理實戰
- Learn SwiftUI
- Linux內核修煉之道
- openEuler操作系統核心技術與行業應用實踐