// 其实不知道这后面加个!号是什么意思
@IBAction func buttonClick(sender: AnyObject) {
let sou = Int(tf.text!)
showLabel.text = String(Shape.init().jishus(sou!))
}
@IBOutlet weak var tf: UITextField!
@IBOutlet weak var showLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// 1.
print(sumof(1,2,3,4,5,6,7,8,9,10))
// 2.show it
showLabel.text = String(sumof(1,2,3,4,5,6,7,8,9,10))
// 3.函数嵌套
func returnFifteen() -> Int {
var y = 10
func add() {
y += 5
}
add()
return y + sumof(1,2,3,4,5,6)
}
print(returnFifteen())
// 4.
func makeIncrementer() -> (Int -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
return addOne
}
let increment = makeIncrementer()
print(increment(7))
// 5.一个函数可以使用另一个函数当参数
func hasAnyMatches(list: [Int] ,condition: Int -> Bool) -> Bool {
for item in list {
if condition(item) {
return true
}
}
return false
}
func lessThanTen(number: Int) -> Bool {
return number < 10
}
let numbers = [20,34,56,2]
hasAnyMatches(numbers, condition: lessThanTen)
print("(hasAnyMatches(numbers, condition: lessThanTen)) is")
// ({})匿名闭包
numbers.map({
(number: Int) -> Int in
let result = 3 * number
return result
})
// 判断是不是奇数
print(jishu(9))
//
var string:String
string = "Hello world"
string .appendContentsOf(" - sososo")
string .appendContentsOf(" == +++++++ gogo")
print(string)
// 调用一个类里的方法
print(Shape.init().simpleDescription())
// 实例化一个类,并给类里的成员赋值,和调用类里的方法
let shape = Shape()
let bol = shape.jishus(100)
shape.numberOfSides = 100
let shapeDescription = shape.simpleDescription()
print("(bol) (shapeDescription)")
// init
let nameshape = NameShape.init(name: "XHSare")
nameshape.numberOfSides = 1000
let names = nameshape.simpleDescription()
print(names)
// 子类继承父类1
let square = Square.init(sideLength: 3.3, name: "my world")
square.sideLength = 1030
print(square.area())
print(square.simpleDescription())
// 子类继承父类2
let circle = Circle.init(r: 9.9, name: "圆")
print(circle.describe())
print(circle.simpleDescription()) //直接调用父类的方法。
// 子类继承父类3 set get方法
let triangle = EquilateralTriangle.init(sideLength: 10.0, name: "天下")
//triangle.perimeter = 100 // newValue 的值 这里调用此方法就会调用set方法
//triangle.sideLength = 10//
print(triangle.simpleDescription())
print(triangle.perimeter)
// enum
let ace = Rank.Jack
let aceRawValue = ace.rawValue
print(aceRawValue)
// 两个枚举类型比较 返回1是左边数大于右边,0是相待 -1是左边数小于右边
print(rankCmp(Rank.Five, num2: Rank.Ten))
// 枚举2
let hearts = Suit.Hearts
let heartDescription = hearts.simpleDescription()
print(heartDescription)
let heartColor = Suit.Hearts
print(heartColor.color())
// 结构体1
let threeOfSpades = Card(rank: .Ace, suit: .Hearts)
let threeOfSpadesDescripthion = threeOfSpades.simpleDescription()
print(threeOfSpadesDescripthion)
// 初识swift Label
let myLabel: UILabel = UILabel.init(frame: CGRectMake(10, 80, 200, 30))
myLabel.textColor = UIColor.whiteColor()
myLabel.backgroundColor = UIColor.blackColor()
myLabel.layer.cornerRadius = 5.0
myLabel.layer.masksToBounds = true
myLabel.layer.borderWidth = 1
myLabel.layer.borderColor = UIColor.redColor().CGColor
myLabel.text = "这是个Label"
self.view.addSubview(myLabel)
}
struct Card {
var rank: Rank
var suit: Suit
func simpleDescription() ->String {
return "The (rank.simpleDescription()) of (suit.simpleDescription())"
}
}
func rankCmp(num1: Rank,num2: Rank) -> Int {
var resultNum: Int = 0
if num1.rawValue > num2.rawValue {
resultNum = 1
}
if num1.rawValue < num2.rawValue {
resultNum = -1
}
if num1.rawValue == num2.rawValue {
resultNum = 0
}
return resultNum
}
enum Suit {
case Spades, Hearts, Diamonds, Clubs
func simpleDescription() ->String {
switch self{
case .Spades:
return "spades"
case.Hearts:
return "hearts"
case.Diamonds:
return "diamonds"
case.Clubs:
return "clubs"
}
}
func color() ->String {
switch self {
case.Spades:
return "black"
case.Clubs:
return "black"
case.Hearts:
return "red"
case.Diamonds:
return "red"
}
}
}
// 枚举
enum Rank : Int {
case Ace = 1
case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
case Jack, Queen, King
func simpleDescription() -> String {
switch self {
case .Ace :
return "ace"
case .Jack :
return "jack"
case .Queen :
return "queen"
case .King :
return "king"
default :
return String("nonono")
}
}
}
class EquilateralTriangle: NameShape {
var sideLength : Double = 0.0
init(sideLength: Double,name :String){
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 44
}
var perimeter: Double { // set get 方法
get {
return 3.0 * sideLength
}
set {
sideLength = newValue / 3.0
}
}
override func simpleDescription() -> String {
return "(name) An equilateral triagle with sides of length (perimeter)."
}
}
class Square : NameShape { // 继承自父类
var sideLength: Double
init(sideLength: Double,name: String) {
self.sideLength = sideLength
super.init(name: name) // 调用父类的init方法
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
override func simpleDescription() -> String { // 重写了父类的方法,必须加override标识
return "(self.name) with sides of length (sideLength). (super.simpleDescription())"
}
}
class Circle: NameShape {
var r:Double = 0.0
init(r : Double,name :String){
self.r = r
super.init(name: name) // 调用父类的init方法 。。为子类的name属性命名
numberOfSides = 999 // 给父类的numberOfSides变量命名
}
func area() -> Double {
return 3.14*r*r
}
func describe() -> String{ //因为两个函数都不是继承自父类的方法所以不用加override
return "(name) have (area()) m2 area it's (numberOfSides)"
}
}
class NameShape {
var numberOfSides: Int = 0
var name : String
init (name : String) {
self.name = name // self.来区分'name'属性和构造器的'name'参数
}
func simpleDescription() ->String {
return "(self.name) with (numberOfSides) sides."
}
}
class Shape {
var numberOfSides = 999
func simpleDescription() -> String {
return "A shape with (numberOfSides) sides"
}
func jishus(number: Int) -> Bool {
if number % 2 != 0 {
return false
}
return true
}
}
func jishu(number: Int) -> Bool {
if number % 2 != 0 {
return false
}
return true
}
func sumof(numbers:Int...) ->Int {
var sum = 0
for number in numbers {
sum += number
}
return sum
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}