Swift 系统学习 20 结构体 添加可失败的构造方法
来源:互联网 发布:c语言debug是什么意思 编辑:程序博客网 时间:2024/06/14 01:17
//: Playground - noun: a place where people can playimport UIKit/* * 本节主要内容: * 1.添加可失败的构造方法 */struct LocationNew { var latitude: Double = 0.0 var longitude: Double = 0.0 // 添加可选型属性 var placeName: String? // nil // 假定参数格式: "39.123,116.456" // 错误参数格式: "39.123*116.456"// init?(coordinateString: String) {// // 获取子字符串// if let commaRange = coordinateString.range(of: ",") {// // 1.获取逗号所在的index// let commaIndex = coordinateString.index(commaRange.lowerBound, offsetBy: 0)// // 2.subString to获取纬度// if let firstElement = Double(coordinateString.substring(to: commaIndex)) {// // 3.subString from获取经度// if let secondElement = Double(coordinateString.substring(from: coordinateString.index(commaIndex, offsetBy: 1))) {// // 经纬度都可以获取// latitude = firstElement// longitude = secondElement// } else {// // 获取经度失败// return nil// }// } else {// // 获取纬度失败// return nil// }// } else {// // 无法获取逗号的index// return nil// }// } // guard语句来优化上面的代码 init?(coordinateString: String) { // 获取子字符串 guard let commaRange = coordinateString.range(of: ",") else { return nil } // 1.获取逗号所在的index let commaIndex = coordinateString.index(commaRange.lowerBound, offsetBy: 0) // 2.subString to获取纬度 guard let firstElement = Double(coordinateString.substring(to: commaIndex)) else { return nil } // 3.subString from获取经度 guard let secondElement = Double(coordinateString.substring(from: coordinateString.index(commaIndex, offsetBy: 1))) else { return nil } // 经纬度都可以获取 latitude = firstElement longitude = secondElement } init(latitude: Double, longitude: Double) { self.latitude = latitude self.longitude = longitude } init() { latitude = 0.0 longitude = 0.0 } // 再加一个构造方法: 包含所有属性(推荐) init(latitude: Double, longitude: Double, placeName: String) { self.latitude = latitude self.longitude = longitude self.placeName = placeName } // 自定义方法 func isNorth() -> Bool { return latitude > 0.0 } func isSouth() -> Bool { return !isNorth() } // 模拟计算两个点的距离 func distanceTo(location: LocationNew) -> Double { return sqrt(pow((self.latitude - location.latitude), 2) + pow((self.longitude - location.longitude), 2)) }}// 实例化, 调用可失败的构造方法// init?本质就是返回LocationNew?// location和locationNew都是LocationNew?类型var location = LocationNew(coordinateString: "39.34,122.3535")var locationNew = LocationNew(coordinateString: "23.34,122.3535")locationNew?.isNorth()locationNew?.distanceTo(location: location!)
0 0
- Swift 系统学习 20 结构体 添加可失败的构造方法
- Swift 系统学习 19 结构体的默认构造方法和自定义构造方法
- 12.6 Swift结构体的构造方法
- Swift 系统学习 18 结构体 改变结构体里面的元素
- Swift基础语法-结构体,结构体构造器,定义成员方法
- Swift 系统学习 22 分析错误的三个阶段 (枚举和结构体相关)
- Swift 基础学习(指定构造方法和便利构造方法)
- Swift 基础学习(构造方法初印象)
- 8.3 Swift结构体构造器
- swift结构体中的构造函数
- C#高级编程学习笔记--------构造函数(层次结构中添加参数的构造函数)
- swift 扩展系统类的构造函数
- 可失败构造函数用法
- Swift 的结构体
- Swift 的结构体
- IOS学习笔记05_1-Swift-如何定义类和类的构造方法、重载方法
- swift: 方法的重载/重写/KVC构造
- 12.3 Swift带有参数的构造方法
- Notification基础详解二(自定义布局通知)
- [看图说话] 基于Spark UI性能优化与调试——初级篇
- Python zip函数介绍
- postman
- java 判断字符串是否为空的四种方法
- Swift 系统学习 20 结构体 添加可失败的构造方法
- $.each(json,function(index,item){ }); 中的2个参数的意思
- 考勤项目查询sql,用于温习,觉得可以的时候要删掉
- 牛腩(7)-关于 CSS 的部分内容
- std::thread详解
- 常用设计模式
- maven+spring测试出现 java.lang.IllegalStateException: Failed to load ApplicationContext异常
- 常用工具方法整理
- Struts2 用 s:if test 判断字符串相等的方法