关于Swift的一点点总结

关于Swift的一点点总结

Welcome to Swift!!

print("Hello World!")

Swift与OC的区别(只适合新手看,因为我也是初出茅庐,不喜勿喷-.-!)

1.swif中输出函数print()会自动换行, 如果想取消换行,何以改成print("", terminator"")
2.swift中函数的格式为:
func 函数名(参数类型)->返回类型{

}
3.swift中的循环结构while{}, repeat{}while(), for index in a...b{}

swift中独有的方法(只写出了一部分)

1.两个字符串相加可以实现字符串拼接,数组也可以做加法如
arr3 = arr2 + arr1
将arr1元素全部添加到arr3中
2.元祖
相当于oc中的属性或者C中的结构体

let stu = (name:String, age:Int, height:Double)

通过元祖还可以实现2个数字值得交换(不需要第三变量)
(a, b) = (b, a)
注:swift中的运算符(+ - * / < >等等)都是一种函数, 并不是单纯地运算符!
3.swift中系统自带排序函数sort()注:括号中可以写参数若sort(>)就是从大到小排序

//冒泡法排序
var arr = [9, 7, 3, 12, 6, 7, 9, 35, 9, 1, 22, 3]

for i in 1.. arr[j+1]{
            (arr[j], arr[j+1]) = (arr[j+1], arr[j])
            iskong = false
        }
    }
    if iskong{
        break
    }
}
print(arr)

下面再附上自己写的简单的小游戏的代码(非常low,可以家里有小孩的人士,供孩子娱乐-.-!)

关于Swift的一点点总结_第1张图片
屏幕快照 2016-08-06 下午3.09.13.png
//石头剪刀布小游戏
import UIKit

class ViewController: UIViewController {
    var imageView:UIImageView!
    var zhuangtai = 0
    var computerNum = Int(arc4random_uniform(3)+1)
    @IBAction func shitou(sender: AnyObject) {
        zhuangtai = 2
        Label3.text = ""
        Label1.text = "已选择"
        Label2.text = ""
    }
    @IBAction func bu(sender: AnyObject) {
        zhuangtai = 3
        Label3.text = "已选择"
        Label1.text = ""
        Label2.text = ""
    }
    @IBAction func jianzi(sender: AnyObject) {
        zhuangtai = 1
        Label3.text = ""
        Label1.text = ""
        Label2.text = "已选择"
    }
    @IBOutlet weak var Label1: UILabel!
    @IBOutlet weak var Label2: UILabel!
    @IBOutlet weak var Label3: UILabel!
    @IBAction func beginButton(sender: AnyObject) {
        print(computerNum)
        //播放动画
        imageView = UIImageView(frame: CGRectMake(0, 0, 150, 150))
        imageView.animationImages = [
            UIImage(named: "shitou")!,
            UIImage(named: "jiandao")!,
            UIImage(named: "bu")!
        ]
        self.view.addSubview(imageView)
        imageView.animationDuration = 0.5
        imageView.startAnimating()
    }
    @IBAction func endButton(sender: AnyObject) {
        imageView.stopAnimating()
        //判断是否赢
        if computerNum == Int(zhuangtai){
            tanchu("平局")
        }
        else if computerNum == 1{
            if zhuangtai == 2{
                tanchu("你赢了")
            }
            else{
                tanchu("你输了")
            }
        }
        else if computerNum == 2{
            if zhuangtai == 3{
                tanchu("你赢了")
            }
            else{
                tanchu("你输了")
            }
        }
        else{
            if zhuangtai == 1{
                tanchu("你赢了")
            }
            else{
                tanchu("你输了")
            }
        }
        let imageStopView = UIImageView(frame: CGRectMake(0, 0, 150, 150))
        //判断是哪张手势
        if computerNum == 1{
            imageStopView.image = UIImage(named: "jiandao")
            self.view.addSubview(imageStopView)
        }
        else if computerNum == 2{
            imageStopView.image = UIImage(named: "shitou")
            self.view.addSubview(imageStopView)
        }
        else{
            imageStopView.image = UIImage(named: "bu")
            self.view.addSubview(imageStopView)
        }
        //动画停止在电脑随机出的结果  所对应的手势
        computerNum = Int(arc4random_uniform(3)+1)
    }
    //调弹窗函数
    func tanchu(str:String){
        var strrr = ""
        if computerNum == 1{
            strrr = "剪刀"
        }
        else if computerNum == 2{
            strrr = "石头"
        }
        else if computerNum == 3{
            strrr = "布"
        }
        let alertC = UIAlertController(title: strrr, message: str, preferredStyle: .Alert)
        let alertAction = UIAlertAction(title: "确定", style: .Default, handler: nil)
        alertC.addAction(alertAction)
        presentViewController(alertC, animated: true, completion: nil)
    }
}

大家如果觉得小编写的对大家有帮助,请"喜欢"我吧-.-!

你可能感兴趣的:(关于Swift的一点点总结)