Swift Struct结构体
来源:互联网 发布:罗振宇人工智能视频 编辑:程序博客网 时间:2024/06/04 18:42
一天一步,快乐前进:
结构体:
结构体就是结构化程序的产物.结构体是由另个或多个类型相同或者不同的数据组成的数据集合,其中的数据或者方法被称为它的成员或成员方法.结构体的成员可以包括属性,类型别名.数组.甚至其他结构体和枚举声明等
不想说太多啦,看code+注释就明白啦....
//// ViewController.swift// struct//// Created by NapoleonBai on 14-11-19.// Copyright (c) 2014年 NapoleonBai. All rights reserved.//import UIKit//定义用户信息结构体struct UserInfo { var userName : String var userAge : Int var userSex : String}/* 如上信息:定义结构体是采用关键字 struct 然后跟上定义名称 在{}中指定成员,并指定数据类型*/class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.managerStruct() } //用来管理结构体 func managerStruct(){ //结构体算是一个复杂的数据类型了 //从C语言开始,就接触过,后来在OC中也使用了,所以,在Swift中也不例外,当然也存在着结构体 //上面我们定义了一个结构体,包含了三个变量类型 //那么,我们怎么使用这个结构体呢? //eg: var userOne : UserInfo? //那使用的时候怎么赋值呢?如下: /* userOne.userName = "曹操" userOne.userAge = 1000 userOne.userSex = "男" */ //上面的赋值形式是通不过的,编译器要求,在创建结构体变量的时候,就必须初始化值,否则不能初始化值,但是可以通过创建的结构体修改值 var userTwo = UserInfo(userName: "曹操",userAge: 1000,userSex: "男") println("the userTwo.userName = \(userTwo.userName)") //the userTwo.userName = 曹操 //这样操作就好了 userTwo.userName = "NapoleonBai" println("update after name = \(userTwo.userName)") //update after name = NapoleonBai //这样更改就没有错误了 //需要注意的是,结构体是值类型的,其实例将会在被赋值变量或常量,被函数调用时被复制 //eg: var userName = userTwo.userName //userName = "NapoleonBai" userName = "曹操" println("the userName = \(userName),the userTwo.userName = \(userTwo.userName)")//the userName = 曹操,the userTwo.userName = NapoleonBai //就是说,我们修改userName的值,并不会影响到userTwo.userName的值 //展示带有构造方法的结构体 var initPoint = Point() println("initPoint.point_y value = \(initPoint.point_y)") //initPoint.point_y value = 34.5 //这里,可以观察Point结构体,只有两个存储坐标的变量 //然后加载两个一个init()方法,这是结构体的构造方法,必须为init,不能变动 /* 说明: 1:swift语言中指定结构体构造函数,是init方法 2:init执行方法,在创建结构体变量之前执行 3:在创建结构体题变量的参数必须和构造函数init 参数必须一致 */ //整理带参数的结构体 var bookInfo = BookInfo(name: "Swift NapoleonBai", price: 1688.8) println(bookInfo.bookName) //Swift NapoleonBai //其实这样就跟创建一个java类对象差不多 //不过这里需要注意的是:这里的变量名称必须同构造方法中的一致 //那么,既然来到这里了,我们就再研究一种;带"_"的构造方法 //如结构体Animal //eg: var animal = Animal("Pander",2) println("animal.animalName = \(animal.animalName)")//animal.animalName = Pander //想必大家都清楚了吧,加"_"就是可以不带名称,但是传值顺序必须相同 //当然了,结构体也是可以在声明的时候就赋值的 //eg: //var initValueOne = initValue(age:23,name:"小白杨") //这样是错误的,因为顺序不一致,跟结构体定义的时候 var initValueOne = initValue(name: "小白杨", age: 23) println("initValueOne = \(initValueOne.name)")//initValueOne = 小白杨 //需要注意的是:初始结构体的时候,赋值顺序一定要一致,在()里面带参数的时候 //当然了,结构体还有更复杂的组成部分,比如说在结构体里面定义方法等等,常见的如get和set方法操作,这些就不一一列举了, //有兴趣的朋友可以查查资料,翻翻书籍啦.... } //定义一个带有构造方法的结构体 struct Point { var point_x : Float var point_y : Float init(){ point_x = 23.4 point_y = 34.5 } } //定义一个带参数的结构体 struct BookInfo { var bookName : String var bookPrice : Float init(name : String,price : Float){ self.bookName = name; self.bookPrice = price; } } //实现带"_"参数的构造方法,具体介绍看上面,嘻嘻 struct Animal { var animalName : String var animalAge : Int init(_ name : String, _ age : Int){ self.animalAge = age; self.animalName = name } } //初始赋值结构体 struct initValue { var name = "NapoleonBai" var age : Int? } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. }}
快快乐乐的学习,然后躺下就睡觉...
0 0
- Swift Struct结构体
- Swift 中的结构体 (Struct)
- 新型结构体---Swift之Struct
- Swift语言精要 - 浅谈结构体(Struct)
- Swift中结构体(Struct)和类(Class)的区别
- swift的struct结构体类型介绍使用
- Swift入门(七)——结构体(Struct)
- Swift-类和结构体(class&struct)
- 结构体(struct) 用法
- 结构体struct module
- 什么结构体 struct
- struct结构体
- struct hostent结构体
- struct hostent结构体
- struct termios结构体
- struct hostent结构体
- 结构体(struct)
- struct termios结构体
- iOS8 定位问题
- 工作流系统关系设计
- Git 基本命令
- apache和tomcat有什么不同,为什么要整合apache 和tomcat?
- 编译器GCC
- Swift Struct结构体
- 解决 ubuntu 14.04.1 下sublime text3 3065 中文输入问题
- Unity学习之五(选择结构)
- 内核双向循环链表之旅
- 黑马笔记3--为应用创建多个快捷方式和intent
- 第1章数组和字符串—6~8题【倒计时23天】
- Atitit.js javascript异常处理机制与java异常的转换.js exception process Voae
- 初学IOS遇到的各种问题汇总
- 开关灯问题