Swift Assertion断言/类型别名/类型转换

一天一点,好好学习

断言是Swift提供出来,供开发阶段捕捉,处理异常信息的一种手段,在发布的时候,系统会自动将其无效化.

所以,我们看看这Assertion是怎么使用滴:

code+注释:

//
//  ViewController.swift
//  assertion
//
//  Created by NapoleonBai on 14-11-20.
//  Copyright (c) 2014年 NapoleonBai. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        managerAssertion()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //用来管理断言
    func managerAssertion(){
        //断言也没用什么很奇特的地方,用起来也比较简单
        /*
        众所周知:基本上每一种语言都拥有自己的异常处理方式,当然OC也有过,不过在iOS开发中,本人还没有用过try-catch来捕获处理异常
        都是将异常信息处理在萌芽期,所以就不用这些;
        当然了,如果是一个很大的项目,那么就肯定不一样了.
        在Swift中,提供了断言这种方式来进行异常捕获,我们可以看看:
        */
        
        //eg:
        var numOne = 22
        
        assert(numOne>=22, "hello ! into assert !")
        
     //   assert(numOne>30, "error! the numOne =\(numOne)")   //ssertion failed: error! the numOne =: file /Users/napoleonbai/Documents/workspace/assertion/assertion/ViewController.swift, line 39
        
        //这样,就能很方便的捕获到异常信息,并且定位到异常位置
        //这个对下标越界有很好地帮助吧
        
        //eg:
        var strOne = "NapoleonBai"
        
    //    assert(strOne.toInt()>=Int.min, "error : the strOne is not Int") //assertion failed: error : the strOne is not Int: file /Users/napoleonbai/Documents/workspace/assertion/assertion/ViewController.swift, line 47
        
        //这里就模拟的一个类型转换错误
        
        /*
        说明:
            断言使用,如果出现了error,那么程序就停止运行,将错误定位到这个地方;
            当然了,断言在发布版本的时候,程序会让断言自动无效化,不影响程序运行效率
            
        使用范围:
            判断数组/集合下标
            判断对象是否已经创建,在使用之前
            验证参数的合法性
        */
        
        //断言的介绍基本上就这样
        //所以,这里补充类型别名和类型转换
        
        typealias shortInt = Int8
        
        println("the shortInt min value = \(shortInt.min)") //the shortInt min value = -128
        
        println("the Int8 min value = \(Int8.min)") //the shortInt min value = -128
        
        //这里可以看出,这个两个值是一样的
        //原因就是 typealias是类型别名的关键字,所谓类型别名就是给类型取了一个另外的名称,比如张三:有的人叫他小张,有得人叫他小三
        
        //类型转换:
        //eg:
        var floatNum = Float(22.4)
        
        println("the floatNum = \(floatNum),the floatNum to Int = \(Int(floatNum))")    //the floatNum = 22.4,the floatNum to Int = 22
        //注意:这里的float类型转换为整型,采用去尾法完成,意思就是不论小数点后面是什么,都去掉,包含*.9这样的数据
        
        //类型转换就是通过Float(),Int(),String()... 等的这样来进行,所以,也没用什么新奇的玩意儿
        
        
        
    }
    

}

开开心心的学习,学无止境,go->


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值