6.swift 用在声明中的关键字

  1 ///////////////用在声明中的关键字/////////////////
  2 
  3 //1.class声明一个类
  4 public class  aClass {
  5     //.class声明一个类函数
  6     public  class func test(let num:Int) ->Void{
  7        
  8     }
  9 }
 10 
 11 //2.deinit反初始化
 12  deinit{}
 13 
 14 
 15 //3.枚举
 16 public enum UIButtonType : Int {
 17     
 18     case Custom // no button type
 19     @available(iOS 7.0, *)//适用于iOS7.0以上,任何平台
 20     case System // standard system button
 21     
 22     case DetailDisclosure
 23     case InfoLight
 24     case InfoDark
 25     case ContactAdd
 26     
 27     public static var RoundedRect: UIButtonType { get } // Deprecated, use UIButtonTypeSystem instead
 28 }
 29 
 30 
 31 //4.extension(扩展 String类)
 32 extension String {
 33     public typealias Index = String.CharacterView.Index
 34     public var startIndex: Index { get }
 35     public var endIndex: Index { get }
 36     public subscript (i: Index) -> Character { get }
 37 }
 38 
 39 //5.func 函数声明
 40  func test(let num:Int) ->Void{}
 41 
 42 //6.import 导入框架或者类
 43 import Foundation
 44 import UIKit
 45 
 46 //7.1 init构造器函数
 47 
 48 // Initializers for creating colors
 49 public init(white: CGFloat, alpha: CGFloat)
 50 public init(hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat)
 51 
 52 //7.2 convenience便利构造器,主要是对一些变量做一些默认值的设置
 53 public convenience init(color: UIColor)
 54 
 55 //8.let 声明一个常量   var 声明一个变量
 56 
 57 //9.protocol 协议
 58 public protocol NSObjectProtocol {
 59     
 60     public func isEqual(object: AnyObject?) -> Bool
 61     public var hash: Int { get }
 62     
 63     public var superclass: AnyClass? { get }
 64     
 65     public func `self`() -> Self
 66     
 67     public func performSelector(aSelector: Selector) -> Unmanaged<AnyObject>!
 68     public func performSelector(aSelector: Selector, withObject object: AnyObject!) -> Unmanaged<AnyObject>!
 69     public func performSelector(aSelector: Selector, withObject object1: AnyObject!, withObject object2: AnyObject!) -> Unmanaged<AnyObject>!
 70     
 71     public func isProxy() -> Bool
 72     
 73     public func isKindOfClass(aClass: AnyClass) -> Bool
 74     public func isMemberOfClass(aClass: AnyClass) -> Bool
 75     @available(iOS 2.0, *)
 76     public func conformsToProtocol(aProtocol: Protocol) -> Bool
 77     
 78     public func respondsToSelector(aSelector: Selector) -> Bool
 79     
 80     public var description: String { get }
 81     
 82     optional public var debugDescription: String { get }
 83 }
 84 
 85 //10.1 static 静态变量
 86 
 87 
 88 //10.2 static 静态函数(在enum和struct,协议中)
 89        class 静态函数(在类中)
 90 
 91/*在非class的类型上下文中,我们统一使用static来描述类型作用域。包括在enum和struct中表述类型方法和类型属性时。在这两个值类型中,我们可以在类型范围内声明并使用存储属性,计算属性和方法。
 92class关键字相比起来就明白许多,是专门用在class类型的上下文中的,可以用来修饰类方法以及类的计算属性。要特别注意class中现在是不能出现存储属性的 94 */
 95 struct Point {
 96     let x: Double
 97     let y: Double
 98     // 存储属性
 99     static let zero = Point(x: 0, y: 0)
100     // 计算属性
101     static var ones: [Point] {
102         return [Point(x: 1, y: 1),
103             Point(x: -1, y: 1),
104             Point(x: 1, y: -1),
105             Point(x: -1, y: -1)]
106     }
107     // 类型方法
108     static func add(p1: Point, p2: Point) -> Point {
109         return Point(x: p1.x + p2.x, y: p1.y + p2.y)
110     }
111 }
112 
113 protocol MyProtocol {
114     static var someTypeProperty: Int {get  set}
static func someTypeMethod()//类方法
} 116 struct MyStruct: MyProtocol { 117 static func foo() -> String { 118 return "MyStruct" 119 } 120 } 121 enum MyEnum: MyProtocol { 122 static func foo() -> String { 123 return "MyEnum" 124 } 125 } 126 class MyClass: MyProtocol { 127 class func foo() -> String { 128 return "MyClass" 129 } 130 } 131 132 //11.新类 SomeClaa
新结构体 SomeStruct 133 // 变量 someProperty 134 // 方法 someFunction 135 class SomeClass{ 136 } 137 138 struct SomeStruct { 139 140 } 141 142 enum SomeEnumeration{ 143 144 } 145 146 protocol SomeProtocol{ 147 148 149 } 150 //要使类遵守某个协议,需要在类型后面加上协议名称,中间以冒号:分隔,作为类型定义的一部分。遵守多个协议时,各个协议之间用逗号,分隔。 151 struct SomeStruct: FirstProtocol,AnotherProtocol{ 152 properties and methods 153 } 154 //如果类在遵守协议的同时拥有父类,应该将父类放在协议名前,以逗号分隔 155 class SomeClass : SuperClass,FirstProtocol,AnotherProtocol{ 156 <#properties and methods#> 157 }
下标脚本语法:subscript(index:Int) ->Int{
get{
//返回与入参匹配的int类型的值
}
set(newValue){
//执行赋值操作
}
}

 //所谓类型别名: 就是指给一个类型取一个别名。这里用到关键字 typealias

typealias myInt = Int8

print(myInt.max) //输出 127 ,这我们取里一个别名代替了系统的 Int8