//函数不仅仅可以当作另一个函数的返回值,还可以当作参数传入到另一个函数
//condition:Int -> Bool作为一个函数传进来
func hasAnyNumbers(name:String,list:[Int],condition:Int -> Bool) -> Bool {
for item in list {
if condition(item) {
return true
}
}
return false
}
func lessThanTen(number:Int) -> Bool {
print(number)
return number < 10
}
var numbers = [1,2,30,4,6,89,10,1,3,5]
hasAnyNumbers("lh",list: numbers,condition: lessThanTen)
numbers.map({
(number:Int) -> Int in
let result = 3*number
return result
})
let mappedNumbers = numbers.map({number in 3*number})
print(mappedNumbers)
//对象和类:使用 class 和类名来创建一个类。类中属性的声明和常量、变量声明一样,唯一的区别就是它们的上下文是 类。同样,方法和函数声明也一样。
class name {//类的名字叫name
func simple() -> String {
print("干嘛")
return "hello everyone"
}
}
//实例化
var myName = name()
myName.simple()
//继承父类,使用init来创建一个构造器,类似oc初始化
class lihuan :name{
var name :String
init(name:String) {
self.name = name;
}
}
var nowName = lihuan(name: "hh")
nowName.simple()
class rWriteName:name{
override func simple() -> String {
return "happy"
}
}
var changeName = rWriteName()
changeName.simple()
//属性的get,set,willSet
//枚举和结构体
enum rank:Int
{
case Ace = 1
case Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten
case Jack,Queen,King
func simple() -> String {
switch self {
case .Ace:
return "ace"
default:
return String(self.rawValue)
}
}
}
let ace = rank.Ace
let aceRawValue = ace.rawValue