# Swift 2 学习笔记 11.结构体

##### 结构体
• 结构体基础
``````struct Location{
let latitude: Double
let longitude: Double
}

let appleHeadQuarterLocation: Location = Location(latitude: 37.3230, longitude: -122.0322)

struct Place {
let location: Location
var name: String
}

``````
• 结构体之构造函数
``````struct Location{
let latitude: Double
let longitude: Double
var placeName: String?

init(coordinateString: String) {
//        let preIndex = coordinateString.range(of: ",")!.lowerBound
//        let sufIndex = coordinateString.range(of: ",")!.upperBound
//        self.latitude = Double(coordinateString.prefix(upTo: preIndex)) ?? 0
//        self.longitude = Double(coordinateString.suffix(from: sufIndex)) ?? 0

let commaIndex = coordinateString.index(of: ",")!
self.latitude = Double(coordinateString[..``````
``` 结构体之可失败的构造函数 struct Location{ let latitude: Double let longitude: Double var placeName: String? init?(coordinateString: String) { if let commaIndex = coordinateString.index(of: ","){ self.latitude = Double(coordinateString[.. 在结构体和枚举中定义方法 struct Location{ let latitude: Double let longitude: Double var placeName: String? init?(coordinateString: String) { if let commaIndex = coordinateString.index(of: ","){ self.latitude = Double(coordinateString[.. 结构体和枚举是值类型 struct Point { var x = 0 var y = 0 } var p1 = Point() var p2 = p1 p1.x += 1 p1.x // 1 p2.x // 0 随处可见的结构体 // Array, Dictionary, Set 都是结构体 // Int, Float, Double, Bool, String 都是结构体 ```
``` ```
``` ```
``` ```
``` ```
``` ```
``` ```
``` ```
``` 你可能感兴趣的 log4j对象改变日志级别 3213213333332132 javalog4jlevellog4j对象名称日志级别 elk+redis 搭建nginx日志分析平台 ronin47 elasticsearchkibanalogstash Yii2设置时区 dcj3sjt126com PHPtimezoneyii2 js实现前台动态添加文本框，后台获取文本框内容 171815164 文本框 持续集成工具 g21121 持续集成 数据结构哈希表(hash)总结 永夜-极光 数据结构 乱七八糟 程序员是怎么炼成的 职场人伤害了“上司” 怎样弥补 aijuans 职场 按字母分类： ABCDEFGHIJKLMNOPQRSTUVWXYZ其他 首页 - 关于我们 - 设为首页 - 加入收藏 - 站内搜索 - Sitemap - 侵权投诉 版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号 ```