2016-03-19 21:29:11 feng2qing 阅读数 2583

 switch后面的()可以省略

 OC中的switch如果没有break就会穿透(依次执行),Swift中不会穿透(可理解默认就有break)

 OC中入股要在case中定义变量,必要要加上{}确定作用域,Swift中不需要

 OCdefault可以在任意位置,在所有case都不满足才会执行default,但是在Swiftdefault只能放在最后

 OC中的default可以省略,Swift中多数情况下不能省略


let i = 0

switch i {

case 1:

    print("1")

    let j = 2

case 2:

    print("2")

case 3:

    print("3")

default:

    print("0")

}


 Swift2.0开始,do while语句发生了变化

 因为do被用作捕获异常.所以do while的写法如下


var num = 0

repeat {

 print(num)

 num++

}while num < 10


2016-06-27 17:51:50 sunchunfeng1 阅读数 201

Swift 循环for&while

  • Swift中的循环和OC中的循环“基本”一致

    • for
    • while
    • do while – > Swiftrepeat whiledo用到异常处理机制中去了)
  • 常规写法

//其他点和if一样
for var i = 0; i < 5; i++ {
    print(i)
}
  • Swift中的写法
//后面的 0..<5 称之为半闭区间
//半闭区间的特点:包含头不包含尾
for i in 0..<5 {
    print(i)
}

//闭区间特点:包含头包含尾
//注意点:.../..< 中间不能有空格
for i in 0...5 {
    print(i)
}

//忽略中间变量
for _ in 0...5{
    print("hello world")
}
  • 后面的 0..<5称之为半闭区间
  • 半闭区间特点
    • 包含头不包含尾
  • 闭区间特点
    • 包含头包含尾
  • 注意点:

    • .../..< 中间不能有空格
  • 注意:

    • Swift 2.2 开始 i++ 系统提示需要使用 i += 1,在Swift 3中会去掉 ++ 写法
    • 警告截图

    • Swift 2.2 开始 for 常规写法( c语言类型写法)系统提示需要使用 Swift 写法 在Swift 3中会去掉常规写法

    • 警告截图

while Swift中很少使用while

var index = 0
while index < 5{
    print(index)
    index += 1
}

repeat while

var i = 0
repeat{
    print(i)
    i += 1
}while i < 5
2016-05-13 17:44:30 android_wxl 阅读数 251

How do I write things in Swift?

Classes

class className {
   func one(){...}
   ...
}

Protocols

protocol protocolName {
   var one: type
   ...
}

Enums

enum enumName {
   case memberValue, anotherMemberValue
   ...
}

Structs

struct structName {
   var one: type
   ...
}

Mutating Properties of a Structure

struct structName {
   mutating func functionName { 
      var one: type
      ...
   }
}

Functions

func functionName(parameters) -> returnType { 
   var one: type
   ...
}

Class-Level Functions

class func functionName(parameters) -> returnType {
   var one: type
   ...
}

Extensions (Categories)

extension classToExtend: optionalProtocol {
   var one: type
   ...
}

In-out parameters

func functionName(inout parameterName: parameterType) { 
   var one: type
   ...
}

Subscripting

struct structToSubscript {
   let constantToSubscript = value

   subscript( parameterName: parameterType ) -> returnType {
      var one: type
     ...
   }
}

Closures

{ (parameters) -> returnType in statements }

{ (parameters) -> returnType in 
   var one: type
   ...
   return result
}

Generics

func genericName<T>(parameterName: T) -> returnType { 
   var one: type
   ...
}

Type-Casting

expression as type
expression as? type

Runtime Type-Checking

expression is type

String interpolation

println("Swift makes me feel \(object) inside")
2016-07-31 18:13:32 chendehao001 阅读数 2808

1. while循环

  • OC的 while 循环

    • 格式:while(循环保持条件){需要执行的语句}
    • 如果只有一条指令while后面的大括号可以省略
    • 条件语句判断非零即真
  • swift的 while 循环

    • while后的圆括号可以省略
    • 只能以bool作为条件语句, 没有非零即真, 只有 truefalse
    • 如果只有条指令while后面的大括号不可以省略
//OC:
//格式:while(循环保持条件){需要执行的语句}
int i = 0;
int sum = 0;
while (i <= 10) {
    sum = i++;
}
//NSLog(@"%d",sum);
//输出结果:10

int i = 0;
int sum = 0;
while (i <= 10)
sum = i++;
NSLog(@"%d", sum);
//输出结果:10
//Swift:
//0.while后的圆括号可以省略
//1.只能以bool作为条件语句
//2.如果只有条指令while后面的大括号不可以省略
var i:Int = 0
var sum:Int = 0
while (i <= 10) {
    sum = i++ // 推荐写法 i += 1
}
print("\(sum)")
//输出结果:10

var i1:Int = 0
var sum1:Int = 0
while i1 <= 10 {
    sum1 = i1++ // 推荐写法 i += 1
}
print("\(sum1)")
//输出结果:10

2. do/repeat while循环

  • Swift2.0之后变为 do while, 变为repeat while, 而do用于捕捉异常
// oc
int i = 0;
int sum = 0;
do {
    sum = i++;
} while (i <= 10);
NSLog(@"%d", sum);
//输出结果:10

int i = 0;
int sum = 0;
do
sum = i++;
while (i <= 10);
NSLog(@"%d", sum);
//输出结果:10
//如果只有一条指令 do 后面的大括号可以省略
// swift
var i2:Int = 0
var sum2:Int = 0
repeat{
    sum2 = i2++ // 推荐写法 i += 1
}while(i2 <= 10)
print(sum2)
//输出结果:10

var i3:Int = 0
var sum3:Int = 0
repeat{
    sum3 = i3++ // 推荐写法 i += 1
}while i3 <= 10
print(sum3)
//输出结果:10
2018-11-20 11:38:08 imhwn 阅读数 695

Swift中使用inline

OC代码编写中我们经常会使用static inline来修饰函数替代,得到更好的性能。
那么在swift中如何使用呢?

@inline(option)

@inline(__always) func foo1() {
    /// do something...
}
@inline(never) func foo2() {
    /// do something...
}

@inline(__always) - will make sure to always inline the function. Achieve this behavior by adding @inline(__always) before the function. Use “if your function is rather small and you would prefer your app ran faster.”
@inline(never) - will make sure to never inline the function. This can be achieved by adding @inline(never) before the function. Use “if your function is quite long and you want to avoid increasing your code segment size.”

使用 @inline(__always)会确保函数总是inline

使用@inline(never)会确保函数inline

我们知道在OC中即使用inline修饰,对编译器来说也只是一个“建议”,具体编译器会自行判断。
那么在swift中如何“建议”呢?

do nothing!啥也不用做,swift会自动判断

下面是例子

func randomInt() -> Int{
    return Int(arc4random_uniform(UInt32.max))
}

func example2(){
    println(randomInt())
}

if we look at the disassembly of example2() we will see this:

                     -[_TtC12swift_weekly14ViewController example2]:
0000000100001550         push       rbp                                         ; Objective C Implementation defined at 0x100004308 (instance)
0000000100001551         mov        rbp, rsp
0000000100001554         push       r14
0000000100001556         push       rbx
0000000100001557         sub        rsp, 0x10
000000010000155b         mov        rbx, rdi
000000010000155e         mov        rax, qword [ds:imp___got__swift_isaMask]    ; imp___got__swift_isaMask
0000000100001565         mov        rax, qword [ds:rax]
0000000100001568         and        rax, qword [ds:rbx]
000000010000156b         lea        rcx, qword [ds:_OBJC_CLASS_$__TtC12swift_weekly14ViewController] ; _OBJC_CLASS_$__TtC12swift_weekly14ViewController
0000000100001572         xor        edi, edi
0000000100001574         cmp        rax, rcx
0000000100001577         cmove      rdi, rbx
000000010000157b         test       rdi, rdi
000000010000157e         je         0x100001593

0000000100001585         mov        edi, 0xffffffff                             ; argument "upper_bound" for method imp___stubs__arc4random_uniform
000000010000158a         call       imp___stubs__arc4random_uniform
000000010000158f         mov        eax, eax
0000000100001591         jmp        0x1000015ad

0000000100001593         mov        r14, qword [ds:rax+0x58]                    ; XREF=-[_TtC12swift_weekly14ViewController example2]+46
00000001000015a7         mov        rdi, rbx
00000001000015aa         call       r14

00000001000015ad         mov        qword [ss:rbp+var_18], rax                  ; XREF=-[_TtC12swift_weekly14ViewController example2]+65
00000001000015b1         mov        rsi, qword [ds:imp___got___TMdSi]           ; imp___got___TMdSi
00000001000015b8         add        rsi, 0x8
00000001000015bc         lea        rdi, qword [ss:rbp+var_18]
00000001000015c0         call       imp___stubs___TFSs7printlnU__FQ_T_
00000001000015cd         add        rsp, 0x10
00000001000015d1         pop        rbx
00000001000015d2         pop        r14
00000001000015d4         pop        rbp
00000001000015d5         ret

Do you see that the code for example2() includes a call to the internal function imp___stubs__arc4random_uniform? but that function was in the randomInt() function, you said with rage! well, swift noticed that the randomInt() function is not long enough to be a separate entity on its own and it’s only been called once in the code so it inlined it to save calling it every time it needs to use it.

now what if we prefix that function with @inline(never)?

@inline(never) func randomInt() -> Int{
    return Int(arc4random_uniform(UInt32.max))
}

let’s disassemble it:

                     -[_TtC12swift_weekly14ViewController example2]:
0000000100001580         push       rbp                                         ; Objective C Implementation defined at 0x100004308 (instance)
0000000100001581         mov        rbp, rsp
0000000100001584         push       r14
0000000100001586         push       rbx
0000000100001587         sub        rsp, 0x10
000000010000158b         mov        rbx, rdi
000000010000158e         mov        rax, qword [ds:imp___got__swift_isaMask]    ; imp___got__swift_isaMask
0000000100001595         mov        rax, qword [ds:rax]
0000000100001598         and        rax, qword [ds:rbx]
000000010000159b         lea        rcx, qword [ds:_OBJC_CLASS_$__TtC12swift_weekly14ViewController] ; _OBJC_CLASS_$__TtC12swift_weekly14ViewController
00000001000015a2         xor        edi, edi
00000001000015a4         cmp        rax, rcx
00000001000015a7         cmove      rdi, rbx
00000001000015ab         test       rdi, rdi
00000001000015ae         je         0x1000015bc

00000001000015b5         call       __TTSf4g___TFC12swift_weekly14ViewController9randomIntfS0_FT_Si
00000001000015ba         jmp        0x1000015d6

00000001000015bc         mov        r14, qword [ds:rax+0x58]                    ; XREF=-[_TtC12swift_weekly14ViewController example2]+46
00000001000015d0         mov        rdi, rbx
00000001000015d3         call       r14

00000001000015d6         mov        qword [ss:rbp+var_18], rax                  ; XREF=-[_TtC12swift_weekly14ViewController example2]+58
00000001000015da         mov        rsi, qword [ds:imp___got___TMdSi]           ; imp___got___TMdSi
00000001000015e1         add        rsi, 0x8
00000001000015e5         lea        rdi, qword [ss:rbp+var_18]
00000001000015e9         call       imp___stubs___TFSs7printlnU__FQ_T_
00000001000015f6         add        rsp, 0x10
00000001000015fa         pop        rbx
00000001000015fb         pop        r14
00000001000015fd         pop        rbp
00000001000015fe         ret

Do you see the call to the __TTSf4g___TFC12swift_weekly14ViewController9randomIntfS0_FT_Si function? that’s the randomInt() function which we disallowed inlining for. it’s working!

rules for using @inline:

  1. use @inline(never) if your function is quite long and you want to avoid increasing your code segment size (use @inline(never))
  2. use @inline(__always) if your function is rather small and you would prefer your app ran faster (note: it doesn’t make that much of a differenence really)
  3. don’t use this keyword if you don’t know what inlining of code actually means. read this first.

swift 4. 字符串

阅读数 5611

在Swift中 声明闭包(Closure)

博文 来自: android_wxl

Swift中的循环语句

阅读数 8890

没有更多推荐了,返回首页