Swift learn 8

Swift学习

Swift语法总结


// Playground - noun: a place where people can play

import UIKit

//协议(Protocol)用于定义完成某项任务或功能所必须的方法和属性,协议实际上并不提供这些功能或任务的具体实现(Implementation)--而只用来描述这些实现应该是什么样的。
//类,结构体,枚举通过提供协议所要求的方法,属性的具体实现来采用(adopt)协议。任意能够满足协议要求的类型被称为协议的遵循者。
//协议类似于java中的interface
protocol SomeProtocol {
    var info : Int { get set }
    var read : Int { get }
}

class Reader:SomeProtocol{
    var info : Int {
        get{
            return 10;
        }
        set{
            
        }
    }
    
    var read : Int {
        get{
            return 10;
        }
    }
}

var reader = Reader();
reader.info;

//method的协议
protocol SuperObj{
    func toString() -> String;
}

class BasicObj:SuperObj{
    func toString() -> String {
        return "className:BasicObj";
    }
}

//使用is和as操作符来检查协议的一致性或转化协议类型。
//@objc用来表示协议是可选的,也可以用来表示暴露给Objective-C的代码,此外,@objc型协议只对类有效,因此只能在类中检查协议的一致性。

//泛型函数可以工作于任何类型
func swapValues<T>(inout a:T, inout b:T){
    let swapV = a;
    a = b;
    b = swapV;
}

var a:Int=10, b:Int=200;

var c:Double=10.0, d:Double=100.0;
swapValues(&a, &b);
swapValues(&c, &d);
println(a);

//范型的stack
struct ObjStack<T>{
    var iteams = [T]();
    
    mutating func push(item : T){
        iteams.append(item);
    }
    
    mutating func pop() -> T{
        return iteams.removeLast();
    }
}

var stack = ObjStack<Double>();
stack.push(0.11);
stack.push(1.09);
println(stack.pop());

//范型版本的数据位置查询
//T: Equatable,也就意味着“任何T类型都遵循Equatable协议”。
func findIndex<T:Equatable>(array: [T], valueToFind: T) -> Int? {
    for (index, value) in enumerate(array) {
        if value == valueToFind {
            return index
        }
    }
    return nil
}

//通过修饰符public、internal、private来声明实体的访问级别
public class SomeP{}

private class SomePrivate();

public class SomePublicClass {           // 显示的 public 类
    public var somePublicProperty = 0    // 显示的 public 类成员
    var someInternalProperty = 0         // 隐式的 internal 类成员
    private var showDetail:String?;
    private func somePrivateMethod() {}  // 显示的 private 类成员
}


//类型转换
class SomeSuperType {}
class SomeType: SomeSuperType {}
class SomeChildType: SomeType {}
let s = SomeType()

let x = s as SomeSuperType  // known to succeed; type is SomeSuperType
//let y = s as Int            // known to fail; compile-time error
//let z = s as SomeChildType  // might fail at runtime; type is SomeChildType?

//where子句中的要求用于指明该类型形参继承自某个类或遵守某个协议或协议的一部分。尽管where子句有助于表达类型形参上的简单约束(如T: Comparable等同于T where T: Comparable,等等),但是依然可以用来对类型形参及其关联约束提供更复杂的约束。如,<T where T: C, T: P>表示泛型类型T继承自类C且遵守协议P。

//泛型类可以生成一个子类,但是这个子类也必须是泛型类。

//泛型形参子句语法
//泛型参数子句 → < 泛型参数列表 约束子句 可选 >
//泛型参数列表 → 泛形参数 | 泛形参数 , 泛型参数列表
//泛形参数 → 类型名称
//泛形参数 → 类型名称 : 类型标识
//泛形参数 → 类型名称 : 协议合成类型
//约束子句 → where 约束列表
//约束列表 → 约束 | 约束 , 约束列表
//约束 → 一致性约束 | 同类型约束
//一致性约束 → 类型标识 : 类型标识
//一致性约束 → 类型标识 : 协议合成类型
//同类型约束 → 类型标识 == 类型标识


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Learn Swift by Building Applications: Explore Swift programming through iOS app development by Emil Atanasov Packt Publishing English 2018-05-25 366 pages 5.0/5.0 1 reviews Details Title: Learn Swift by Building Applications: Explore Swift programming through iOS app development Author: Emil Atanasov Length: 366 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2018-05-25 ISBN-10: 178646392X ISBN-13: 9781786463920 Sales Rank: #1123253 (See Top 100 Books) Categories Computers & Technology Mobile Phones, Tablets & E-Readers Operating Systems Programming Programming Languages Description Start building your very own mobile apps with this comprehensive introduction to Swift and object-oriented programming Key Features A complete beginner's guide to Swift programming language Understand core Swift programming concepts and techniques for creating popular iOS apps Start your journey toward building mobile app development with this practical guide Book Description Swift Language is now more powerful than ever; it has introduced new ways to solve old problems and has gone on to become one of the fastest growing popular languages. It is now a de-facto choice for iOS developers and it powers most of the newly released and popular apps. This practical guide will help you to begin your journey with Swift programming through learning how to build iOS apps. You will learn all about basic variables, if clauses, functions, loops, and other core concepts; then structures, classes, and inheritance will be discussed. Next, you'll dive into developing a weather app that consumes data from the internet and presents information to the user. The final project is more complex, involving creating an Instagram like app that integrates different external libraries. The app also uses CocoaPods as its package dependency manager, to give you a cutting-edge tool to add to your skillset. By the end of the book, you will have learned how to model real-world apps

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值