swift简介

一.swift的基本格式:

1.没有.h.m文件,所有的声明和实现都写在.swift文件中

建议:所有的代码需要写在{}中

2.每行的结尾没有; 


二.OC和swift的语法的区别:
(1)oc中实例化:alloc init; alloc initWithXXX
swift 中 使用()   类名(XXX)
(2)oc中  [UIColor redColor]
swift中 UIColor.redColor
(3)枚举:点分开  快捷  enter -> 右键 -> 点
可以省略前半部分(有时候没有智能提示)
(4)在当前的本类中调用自己的属性或方法  不需要添加self;建议不要添加self,在后边的闭包中必须添加self
(5)打印:print()

(6)运算符左右必须加空格分开


三.常量和变量:
let:表示常量 一经赋值,不可修改  不可变

var:表示变量  可变

:开发中  推荐使用let  因为let更加安全;在需要修改的时候在改成var


在swift中  数据的类型是自动推导,根据=号右边来确定数据的具体类型
swift是一个类型校验  非常严格的语言


四.数据类型:

整数默认的类型是int long
小数默认的类型是Double 双精度  CGFloat 单精度
不同类型的数据无法惊醒计算,不支持隐式转换

(1)将Double类型转换成CGFloat类型
let a: CGFloat = 20.5


五.可选项:
定义:let a: Int?(问号表示可选类型)
赋值:let a:Int? = 10;(有值就是具体类型的值,没有就是nil)
打印时会带上optaional
打印:
当没有值时,不可使用print(a!)  ->  会直接崩溃(原因:在使用强制解包时,可能会有风险  -> 所以每次使用!就得考虑是否安全 -> 使用分支结构)
当有值时,可以使用print(a!),打印出来就只有数据,没有optaional
:所有的可选项都不可直接参与运算


??(运算符优先级低):快速判断是否为空,并且设置默认值(??后边的数据)
例如: var a: Int?
print(a ?? -1 + 20)  -> 19


六.分支结构:
(1)条件:没有();  {}不可省略
(2)在swift中,没有非0即真的概念(c语言中的概念),只有true和false


七.if的实际运用:

(1)if let: 表示赋值,并且判断是否为空
(2)guard let ... else:守卫


八.switch:
在swift中  break可以省略;每一个case中,必须至少有一个可执行的代码
在case中定义变量,不需要加{};case的类型可以是任意类型(在oc中  case的类型 必须是)
在case中可以同时添加多个条件

九.循环:
(1)for循环
循环的条件可以省略()
for var i = 0; i < 10; i++ {
}
(2)for in循环
0..<10:表示循环范围(不包含10)
0...10:表示0~10范围(包括10)
for i in 0..<10 {
}
当没有调用i时,会提示用_(忽略)代替

十.字符串:
oc中  NSString  继承NSObject  是一个对象  效率较低 不支持遍历
swift中  String  是结构体   更加高效 支持遍历
(1)字符串的长度
取得是字符编码的长度(中文的字符编码的长度为3)
想要汉字的个数用count
(2)字符串的拼接
let str = str1 + str2

print("\(str1)便是晴天")
\() 用于转译字符串
(3)字符串的比较
if str1 == str2 {
}else {
}
(4)字符串和range
 截取子串
将String转换成NSString 再去截取子串
let subStr = (str as NSString).substringWithRange(NSMakeRange(6,2))

十一.数组:
swift中的数组:[元素]
数组中不建议存放类型不相同的对象
(1)添加元素

arr.append("元素")

:移除中间的元素时,需要判断一下是否有元素
(2)数组的拼接
let arr = arr1 + arr2

十二.字典:
oc中定义字典使用{}
swift中定义字典使用[key : value]
字典是无序的

(1)修改字典:根据key修改value
(2)遍历:for(key, value) in dict{
print("key = \(key)")
print("vslue = \(value)")
}

十三.函数:
格式:func 函数名(参数: 参数类型) -> 返回值类型
例如:
let result = sum(10, b: 100)
func sum(a:Int, b: Int) -> Int {
return a + b
}
(1)调用:第一个参数名可以被省略
函数的外部参数(能够让外界调用,对函数的语义更加清晰;在函数内部使用更加简洁)
func sum(height a:Int, width b: Int) -> Int {
return a + b
}
(2)函数没有返回值的三种写法:

(1)func demo(str: String) -> Void {
}
(2)func demo(str: String) -> () {
}
(3)func demo(str: String) {
}

十四.block闭包:
block的特点:提前准备好的一段代码;可以当做参数传递;在需要的时候执行block;在block中使用self有可能使用循环引用
运用场景:在网络异步耗时 回调

接触循环引用:最重要的一点打破强引用循环
__weak typeof(self) weakSelf = self
__weak作用:当对象被系统回收的时候,对象的地址u自动指向nil

:循环引用并没有那么容易出现
只有在有成员变量或属性对外布的block进行记录  这时候就徐璈考虑循环引用

swift中的闭包
闭包的简写 :当函数的最后一个参数是闭包,可以将闭包简化为'尾'随闭包
闭包的特点:提前准备好的一段代码;可以当做参数传递;在需要的时候执行闭包;在闭包中访问self可能会出现循环引用

定义闭包:
无参无返回值var finished:() -> ()
() -> () :表示没有参数,没有返回值的闭包;第一个()表示闭包的参数; -> 表示闭包的返回值
在闭包中调用自己的方法或属性的时候使用self

循环引用的解决办法:
(1)oc的解决办法
weak var weakSelf = self
闭包里使用时:
print(weakSelf?.view)
(2)swift的解决办法
在表示参数的()前边加上[weak self]--与weak的作用是一样的 -> __weak
:表示闭包中访问的所有的self都是弱引用
在闭包使用的时候系统会自动加上?(原因:有可能指向nil)
(3)在参数的()前加上[unowned self]--与assgin的作用是一样的 -> unsafe_unretained
:对象回收的时候不会自动指向nil,会有风险造成野指针

十五.面向对象:
oc中的initWithXXX{
}
在swift中是override init() {}:表示重写父类的构造函数
花括号里:name = "刘亦菲"
super.init()
在构造方法中是先构造子类在构造父类
先调用子类,再调用父类
super.init表示构造函数的完成
若没有写super.init,则会隐式生成(在Xcode7base5开始,在之前的版本都需要手动加上)-->只是针对系统的super.init,若是在父类重写了super.init,在子类必须调用,所以最好都手动调用super.init

重写:父类已经有了  ,在子类上进行扩展

重载:函数名相同,参数的类型以及参数的个数不同就形成重载,是面向对象最显著的标志
构造函数的责任:一定构造一个对象出来
当属性名和参数名相同时,需要加上self来区分
一旦重载了构造函数,默认的构造函数就不能访问了


十六.KVC构造
是oc特有的机制,oc可以和swift共存
在swift中表示包含所有的类型用:AnyObject
在swift,一旦程序崩溃在main函数的时候,就一定是oc出现问题

运行是给对象转发setValue: forKey:


在 swift中,所有的类和类的方法都是共享的
同一个命名空间(项目名称)  所有的类都是共享的

十七.懒加载:
在swift中,懒加载有特殊的写法
简写:
lazy var nameLabel: UILabel = UILabel()
要是需要初始化一些属性,需要加上{},在里边写上要的初始化代码
实际上懒加载就是闭包

十八.在oc中调用swift,需要导入桥接文件,命名空间-Swift.h 文件
#import"单例-Swift.h"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swift iOS Programming for Kids by Steffen D. Sommer English | 22 Mar. 2017 | ASIN: B01N6E8EB9 | 296 Pages | AZW3 | 11.33 MB Key Features Children can express their creativity while learning through interactive Swift Playgrounds Empower children to think critically about problems Learning programming basics can help children gain confidence in problem solving Help children put their imagination into action building their first iOS app Book Description This book starts at the beginning by introducing programming through easy to use examples with the Swift Playgrounds app. Kids are regularly encouraged to explore and play with new concepts to support knowledge acquisition and retention – these newly learned skills can then be used to express their own unique ideas. Children will be shown how to create their first iOS application and build their very own movie night application. What you will learn Basic programming and coding fundamentals Write code using the fun and interactive Swift Playgrounds app Make animations, including creating your own starry night Utilise functions by making pizza in code Create an interactive toy bin Learn how to use control flow statements to further enhance your toy bin Build a simple movie night app working with tableviews and arrays About the Author Since Swift was announced at WWDC, Steffen D. Sommer has had a passionate interest in the programming language. He's currently working as a lead Vapor developer at a company called Nodes in Copenhagen, where he focuses on developing backend systems using Swift. In his spare time, he helps organize the local iOS meet up, visits iOS conferences around the world, and explores the different aspects of and use cases for Swift, such as putting Swift on the server and doing functional programming in Swift. You can also find him contributing to open source projects on GitHub or blogging on his personal website. Jim Campagno is an iOS developer and teacher living in New York City. He's currently working as an iOS instructor at the Flatiron School, helping beginners of Swift and iOS become iOS developers. Jim has a deep desire and high level of creativity that he brings to teaching. He created the Swift online course offered at Flatiron School, which includes in-depth readings along with test-driven labs, challenging the student to write code in Swift. Jim also runs an active YouTube channel, putting out in-depth content and helping students understand everything in iOS and Swift—from the basics to complex topics. Most importantly, Jim ensures that the content he creates is accessible, fun, and interactive. He enjoys putting together a story behind every topic to make it more enjoyable for the reader. Table of Contents What is Programming? Getting Set Up Say Hello Favorite Things Factories Making Pizza Toy Bin Smarter Toy Bin Making Some Friends Pokemon Battle Simon Says Starry Night Space Pizza Delivery Movie Night - iOS App
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值