Swift 系统学习 20 结构体 添加可失败的构造方法

本文介绍如何在Swift中使用可失败构造方法初始化结构体LocationNew,并通过实例演示了如何判断地理位置位于南半球还是北半球及计算两点间距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//: Playground - noun: a place where people can play

import 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!)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值