-
Notifications
You must be signed in to change notification settings - Fork 0
swift学习笔记
@(swift)[swift] #learn the swift
- get the different of the heap and stack : (1). every class is in the heap
- option
- core os -> core service ->media -> cocoa touch
- framworks : Foundation and UIKit :Core Data,Map Kit,Core Motion
- 元组在临时组织值的时候很有用,但是并不适合创建复杂的数据结构,如果你的数据结构并不是临时使用,请使用类或结构体而不是元组。
(1). !
获取可选值的值,可选值的强制解析
let possibleNumber = "123" let convertNumber = possibleNumber.toInt() //假设convertedNumber是int可选值 println(convertedNumber) //Optional(123) println(convertedNumber!) //123
(2). optional binding 可选绑定
let actualNumber = possibleNumber.toInt() //actualNumber 只被用来转换结果
(3). 可选变量赋值为nil来表示它没有值,不用用于非可选的常量和变量。如果你的代码中有常量或者变量需要处理缺失的情况,请把他们声明成对应的可选类型。nil不是指针,是一个确定的值,用来表示缺失。任何类型的可选状态都可以被设置为nil。
var serverReponseCode:Int? = 404
(4). 隐式解析可选类型
把想要用作可选的类型的后面的问号(string ?)改成感叹号(string !)来声明一个可选隐式>解析类型。
- nil coalescing operator 空和运算符( a??b)
对可选类型a进行空判断,相当于
a!=nil?a!:b
- 区间运算符 a...b 半区间运算符 a..<b
- 短路计算 short-circuit evaluation
- 逻辑运算符,通过括号来明确优先级,增加可读性
- 字符串和字符 String Character,快速的兼容Unicode的方式来处理代码中的文本信息。 (1).String Literals 字符串字面量
//空字符串字面量
var emprtyString = ""
//初始化String实例
var anotherEmptyString = String()
###swift 基本语法
//1.输出
print( "Hello, playground")
//2.常量与变量
var a="我是变量"
let b="我是常量"
//3.指明类型
let letInteger :int_fast32_t = 70;
let letDouble :Double = 70.0;
let letString :NSString = "HelloSwift"
//4.转换字符串:String()或\()
let myString = "myInt is "
let myInt = 94
let myString2 = myString + String(myInt)
let myString3="myInt is \(myInt)"
//5.数组创建与调用
var array = ["one", "two", "three", "four"]
var getTwo=array[1];
//6.数据字典创建与调用
var dictionary=["oneName":"I am one value","twoName":"I am two value"];
var getTowValue=dictionary["twoName"];
//7.for语句
for item in array
{
var i=item;
}
//8.函数
func getUserName(loginName:String)->String
{
return"Lily";
}
//9.枚举
enum Week {
case 星期一
case 星期二
case 星期三
case 星期四
case 星期五
case 星期六
case 星期天
}
//10.Switch语句
var today="星期一"
switch today{
case "星期一":
print("今天是星期一");
case "星期二":
print("今天是星期二");
default:
print("不知道今天星期几");
}
//11.类
class Person :NSObject
{
var userName:String;
var userAge=0;
override init()
{
userName="";
}
}
PLAYGROUND 可视化 由 王巍 (@ONEVCAT) 发布于 2015/09/23
//冒泡排序算法
var arr = [14, 11, 20, 1, 3, 9, 4, 15, 6, 19,
2, 8, 7, 17, 12, 5, 10, 13, 18, 16]
func plot<T>(title: String, array: [T]) {
for value in array {
XCPCaptureValue(title, value: value)
}
}
plot("起始", array: arr)
func swap(inout x: Int, inout y: Int) {
(x, y) = (y, x)
}
func bubbleSort<T: Comparable>(inout input: [T]) {
//T必须遵守Comparable协议,即可比性协议
for var i = input.count; i > 1; i-- {
var didSwap = false
for var j = 0; j < i - 1; j++ {
if input[j] > input[j + 1] {
didSwap = true
swap(&input[j], &input[j + 1])
}
}
if !didSwap {
break
}
plot("第 \(input.count - (i - 1)) 次迭代", array: input)
}
plot("结果", array: input)
}
bubbleSort(&arr)
Swift/iOS: How to use inout parameters in functions with AnyObject/Any or Pointers
func setValue<T>(inout object: T, key: String) {
switch key {
case "String":
object = ("A String" as? T)!
case "UIColor":
object = (UIColor.whiteColor() as? T)!
case "Bool":
object = (true as? T)!
default:
print("Unhandled key: \(key)")
}
}
var string: String = "Default String"
var color: UIColor = UIColor.blackColor()
var bool: Bool = false
setValue(&string, key: "String")
setValue(&color, key: "UIColor")
setValue(&bool, key: "Bool")
/*
If you are going to force-cast (and please don’t, there are better solutions), you can do value as! Thing rather than (value as? Thing)!. – Airspeed Velocity Apr 30 at 9:52
1
This is what I was looking for! Thanks a lot! – Django Apr 30 at 9:54
I was struggling with the same error message as the OP, so thank you for this answer! I still don't quite understand why we need to use a generic function if we're just trying to cast a subclass to its superclass, though. – Alexander Oct 1 at 19:59
*/
###swift's changes
//swift's changes
//old
var str = "12"
var val1 = str.toInt()
//new
var str = "12"
var intValue = Int(str)
###swift tips
- 与effective-c 2.0说法有出入,dan
虽然说使用 enumerateObjectsUsingBlock: 非常方便,但是其实从性能上来说这个方法并不理想 (这里有一篇四年前的星期五问答阐述了这个问题,而且一直以来情况都没什么变化)。另外这个方法要求作用在 NSArray 上,这显然已经不符合 Swift 的编码方式了。在 Swift 中,我们在遇到这样的需求的时候,有一个效率,安全性和可读性都很好的替代,那就是快速枚举某个数组的 数组 ENUMERATE
- control transfer statement
- continue
- break
- fallthrough
- return
- throw
######continue(loop) "I am done with the current loop iteration" without leaving the loop altogether.
######break(switch,loop) ends excutions of an entire control flow statement immediately.
######labeled statement
labeled name: while condition{
statement
}
######early exit
guard
You use a guard
statement to require that condition must be true in order for the code after the guard
statement to be executed.
######checking API availability
if #available(iOS9,*){
}else{
}