//
Main.swift
// Closed Package
//
//Created by Zhu Min On 15/6/15.
//Copyright (c) years Helinyu. All rights reserved.
//
Import Foundation
Let names = ["Lily","Anna","Duty","Poly" ,"Gray"]
func comparename (S1:string , s2:string) Bool {return s1 < s2}
var sortedarray = names.sorted (comparename)
//println (" changes after permutation:\ (sortedarray)");
var Sortedarray = names.sorted ({(S1:string, s2:string),Bool In return s1 < s2})
println (" changes after permutation:\(sortedarray)" );
Let A = 5
Let A = 4
println (a)
// The following function implements the addition of two string variables
func SayHello (S1:string , s2:string) {
var str = s1 +"" + s2
println ("The values is:\(str)")
}
SayHello ("Nihao","Helinyu")
/*
1:func is a function keyword
2:say is the function name, which represents the address of the function
3:say function parameter is an integer return value of Boolean type
*/
func Say (num:Int) Bool {
return num >ten
}
var by : ( Int ),Bool = Say
/*
1 : Say is the address of the function name, and the function type, parameter is shaping, return value Boolean type
2 : ( Int)->bool represents the function type, the parameter is shaped , and the return value is a Boolean type.
namely: by is a function variable (the parameter is shaped and the return value is a Boolean type) point to function name (Say)
*/
var B = by (ten) //by (a) calling functions directly
println (b)
/*
function Getlist Description
Parameters:
1 : The first parameter Shaping set variables
2 : Second argument, function type variable parameter is an integer return value Boolean type
return value
Shaping a Collection
*/
Func GetList (arr:int[], pre: (Int)->bool)->int[]{
// // defines an empty variable-shaping collection
var Temparr = int[] ()
//
For temp in arr {
If pre (temp) {
Temparr + = temp;
// }
// }
return Temparr;
//}
//
///*
// called Getlist Description
// first parameter Shaping an array [1,2,3,4]
// a second parameter The closure to point to the function type.
//{(s) in return s>2} closure type description, parameter is shaping , The return value is a Boolean type
//*/
Let Arr=getlist ([1,2,3,4],{(S:int) in return s>2})
println (arr)
// in class " Instance Properties " when there is no initialization, the compilation is not over.
// Solution: 1) Initialize when defining variables 2 Initialized using the initialization method
class Rect
{
var width:int = 0;
var height:int = 0;
// or
var width:int;
var height:int;
//
// // Initialize Method
Init ()
// {
width = 0;
Height = 0;
// }
//3) or
var width:Int;
var height:Int;
init(w:int, h:int)
{
width = w;
Height = h;
}
func setSize (w:int, height:int)
{
width = w;
self. height = height;
}
Func Getarea ()-int{
return self.width * height;
// }
}
var rect = rect ();
Rect.setsize (ten, height:20);
var rect = rect (w:ten, H:)
println ("The area obtained is:\(rect)")
println ("The area obtained is:\(rect)")
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Closures in Swift