iOS Swift Language Reference

 Author: Shengtao Zhou       Created: 7/7/2019 6:27:42 AM       Modified: 7/7/2019 3:26:36 PM   More...
XCode Playground project is available from iOS 8. Developers can use this project type to try Swift statements and view result interactively.

Swift statements are not required end with semicolon (;), but you still can if you wish.

Data Type:

Const (Value can't be changed after assigned)
   let score = 95
   let score :Int = 95

Variable (Value can be changed)
   var name: = "Tim"
   var name:String = "Tim"

Optional (Value can be nil)
   var mathScore : Int? = 90
   mathScore = nil

Common Data Types
Int, Float, Double, Bool, String, Character, Tuple, Class, struct, enum, Collections 

Collections:

Collection types include Array, Set and Dictionary.

String operation:

  var name = "Tony"
  var score = 96
  var message = "\(name)'s score is \(score)."
  message = name + "'s score is " + String(score)

Tuple operation:

  let applicantDetails = ("Henry", 80)
  print("Applicant name is \(applicantDetails.0)")
  print("Applicant score is \(applicantDetails.1)")

Function:

func concat<T1, T2>(a: T1, b: T2) -> String {
    return "\(a)" + "\(b)"
}

let message = concat("Tony's score is", 96)

Define Operator:

operator infix +++ {}
@infix func +++ <T1, T2>(a: T1, b: T2) -> String {
    return concat(a, b)
}

let message = "Tony's score is" +++ 96

Control Flow:

The condition of control flow doesn't need to be quoted in Swift, but you can if you like.

If / Else:
   if score > 85 // or "if (score > 85)"
   {
       print("Good job, you've got an A!");
   }else{
       print("Good job, but you can be better.");
   }

Switch: 
   let type = 1;

   switch type {

   case 1:
      print("You have an apple.")

   case 2,3,4,5:
      print("You have an orange.")

   default :
      print("You have a fruit.")
   }

For:

   let places = ["San Francisco", "Seattle", "Vancouver"]
   for place in places
   {
       print("\(place) is a nice city to live.")
   }

While:
   var number = 1
   while number < 10
   {
       print("This is the \(number) round.")
       number = number + 1;
   }

Repeat:
   var number = 1
   repeat
   {
       print("This is the \(number) round.")
       number = number + 1
   }
   while number < 5

Loop control with "break" and "continue":
   var number = 1
   while number < 10
   {
       if number = 5 {
   print("This is the \(number) round. You're in the middle")
   continue
       }
       number = number + 1;
       print("This is the \(number) round.")
       if number = 10 {
    print("The end")
      break
       }
   }

Closure:
Closures are self-contained blocks of functionality that can be passed around and used in your code. 

struct Student {
  var Name: String
  var Score: Int
}

var students = [
  Student(Name: "Leonard", Score: 85),
  Student(Name: "Michael", Score: 100),
  Student(Name: "Charles", Score: 75),
  Student(Name: "Michael", Score: 90),
  Student(Name: "Alex", Score: 78),
]

//Closure function
contacts.sort {
  $0.Score > $1.Score
}

print(students)


More...          Back to List          

(Please enter you comments between 100 to 2000 characters. Thanks for your contribution.) 

         Created:       Modified: 

Editing a comment

       (Please enter you comments between 100 to 2000 characters. Please login before edit comment.)