Swift 学习笔记-(一)

常量与变量

常量 let, 

let name = "John"
//常量赋值后不可修改
name = "Price" // 报错 提示let常量不能修改值

变量 var

var age = 10
//变量可修改
age = 11

print(age)

字符串基本操作

字符串创建方式

var name = "John"
var text1 = ""
var text2 = String()
var text3 = String(repeating: "a", count:10)

多行字符串使用三个双引号

var text4 = """
A
B
C
"""

单行字符串使用“\“符号进行多行拼接

var text5 = """
A\
B\
C\
"""

字符串转义

//第一种转义 不建议复杂容易导致看不懂
var text6 = "\"A\""

//第二种
var text7 = #""A‘B""#

字符串连接

//第一种使用+符号进行字符串拼接
var name = "John"
var age = "32岁"
print(name+age)

//第二种使用appending
print(name.appending(age))

//第三种运算符
name+=age

//第四种 不同类型的数据与字符串拼接使用转义字符\()
var age = 32
print("\(name)年龄:\(age)岁")

字符串的属性

//字符串是否为空
print(name.isEmpty)

//字符串长度
print(name.count)

//字符串描述(值)
print(name.description)

//便于调试的值
print(name.debugDescription)

//输出字符串哈希值
print(name.hashValue)

//字符串大小写
print(name.uppercased())//字符串转大写
print(name.lowercased())//字符串转小写

//字符串比较
print("ab" == "ab") //相等 true
print("ab" != "ab") //不等 false

//字符串判断开头/结尾是否包含某个字符串
.hasPrefix("***") //开头是否包含
.hasSuffix("***") //结尾是否包含

//字符串输出索引
name.indices

//字符串索引位 与OC不同 不是显示从0~last 而是 String.startIndex、String.endIndex、String.index(before.index)、String.index(after:index)、String.index(index.offsetBy:5)


整数

绝对值

//整数的绝对值
var x = -21
x.negate() // 21
x.magnitude // 21 建议使用
abs(-21) // 21

求商和余数

var x = 100
let (a,b) = x.quotientAndRemainder(dividingBy:8)

平方根

var a = 10
a.squareRoot()

四舍五入

//默认 rounded():四舍五入到最近整数

print(3.2.rounded())   // 3.0
print(3.5.rounded())   // 4.0 (四舍五入)
print(3.7.rounded())   // 4.0
print(-2.3.rounded())  // -2.0
print(-2.5.rounded())  // -3.0 (注意负数的四舍五入)

//.awayFromZero:幅度大于或等于原值的舍入

//规则:总是向远离零的方向舍入(正数向上,负数向下)

print(3.2.rounded(.awayFromZero))  // 4.0 (幅度 4.0 > 3.2)
print(3.7.rounded(.awayFromZero))  // 4.0 (幅度 4.0 > 3.7)
print(3.0.rounded(.awayFromZero))  // 3.0 (幅度 3.0 == 3.0)
print(-2.3.rounded(.awayFromZero)) // -3.0 (幅度 3.0 > 2.3)
print(-2.7.rounded(.awayFromZero)) // -3.0 (幅度 3.0 > 2.7)

//.down:幅度小于或等于原值的舍入

//规则:向数轴负方向舍入(向下舍入)

print(3.2.rounded(.down))  // 3.0 (幅度 3.0 < 3.2)
print(3.7.rounded(.down))  // 3.0 (幅度 3.0 < 3.7)
print(3.0.rounded(.down))  // 3.0 (幅度 3.0 == 3.0)
print(-2.3.rounded(.down)) // -3.0 (幅度 3.0 > 2.3)
print(-2.7.rounded(.down)) // -3.0 (幅度 3.0 > 2.7)

//.up:向数轴正方向舍入(向上舍入)

print(3.2.rounded(.up))  // 4.0 (幅度 4.0 > 3.2)
print(3.7.rounded(.up))  // 4.0 (幅度 4.0 > 3.7)
print(-2.3.rounded(.up)) // -2.0 (幅度 2.0 < 2.3)
print(-2.7.rounded(.up)) // -2.0 (幅度 2.0 < 2.7)

//.towardZero:向零方向舍入(截断小数)

print(3.2.rounded(.
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、付费专栏及课程。

余额充值