How do I write things in Swift?

前端之家收集整理的这篇文章主要介绍了How do I write things in Swift?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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")
原文链接:https://www.f2er.com/swift/323788.html

猜你在找的Swift相关文章