Swift3.0 Optional String 类型 强制拆包

        /**

         Swift3.0 String OptionalString作为urlString去初始化url并进行网络访问时,会出现urlOptionalstring的错误情况

         如:在AFNetwork3.0get请求中使用Optional 类型的 String时,会有如下错误:

         //NSErrorFailingURLStringKey=Optional(http://api.test.com/api/getcommon.php)?bundleId=com.dddd

         所以要避免urlStringOptional类型 

         String类型转为Optional String类型为 封包

         Optional String 类型强制转换为String类型 成为强制拆包

         */

        

        //封包 显式封包

        let urlStrOptional: String? = "http://api.test.com/api/getcommon.php";

        print("the urlStrOptional is \(urlStrOptional)")

        //输出为:the urlStr is Optional("http://api.test.com/api/getcommon.php") 不能作为urlString 去发送url请求

        


        //封包 隐式封包

        //此处需要注意虽然在定义urlStr时定义为非Optional类型但是Swift3.0在使用 '=' 赋值时,左边的变量会被自动转换为Optional类型(称为隐式封包)

        //这点是Swift3.0的一个特点 需要特别注意!!!

        let urlStr: String! = "http://api.test.com/api/getcommon.php";

        print("the urlStr is \(urlStr)")

        //输出为:the urlStr is Optional("http://api.test.com/api/getcommon.php") 不能作为urlString 去发送url请求

        

        //强制拆包

        //String被隐式封包时 如果要作为初始化URLString时,必须要进行强制拆包,强制拆包为 Optional类型的String参数后加上 '!'

        //上面的 urlStr 已经被隐式封包 此处进行强制拆包

        //urlStrOptional! urlStr! 可作为初始化URLString 因为urlStrOptional! urlStr!进行了强制拆包

        print("the urlStrOptional is \(urlStrOptional!)")

        print("the urlStr is \(urlStr!)")


//
//  ViewController.swift
//  test_swift3.0_Optional_01
//
//  Created by jeffasd on 17/3/15.
//  Copyright © 2017年 jeffasd. 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.
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        /**
         Swift3.0中 String的 Optional在String作为urlString去初始化url并进行网络访问时,会出现url为Optional包string的错误情况
         如:在AFNetwork3.0的get请求中使用Optional 类型的 String时,会有如下错误:
         //NSErrorFailingURLStringKey=Optional(http://api.test.com/api/getcommon.php)?bundleId=com.dddd
         所以要避免urlString为Optional类型 
         将 String类型转为Optional String类型为 封包
         将Optional String 类型强制转换为String类型 成为强制拆包
         */
        
        //封包 显式封包
        let urlStrOptional: String? = "http://api.test.com/api/getcommon.php";
        print("the urlStrOptional is \(urlStrOptional)")
        //输出为:the urlStr is Optional("http://api.test.com/api/getcommon.php") 不能作为urlString 去发送url请求
        

        //封包 隐式封包
        //此处需要注意虽然在定义urlStr时定义为非Optional类型但是Swift3.0在使用 '=' 号 赋值时,左边的变量会被自动转换为Optional类型(称为隐式封包)
        //这点是Swift3.0的一个特点 需要特别注意!!!
        let urlStr: String! = "http://api.test.com/api/getcommon.php";
        print("the urlStr is \(urlStr)")
        //输出为:the urlStr is Optional("http://api.test.com/api/getcommon.php") 不能作为urlString 去发送url请求
        
        //强制拆包
        //当String被隐式封包时 如果要作为初始化URL的String时,必须要进行强制拆包,强制拆包为 在Optional类型的String参数后加上 '!' 号
        //上面的 urlStr 已经被隐式封包 此处进行强制拆包
        //urlStrOptional! 和 urlStr! 可作为初始化URL的String 因为urlStrOptional! 和 urlStr!进行了强制拆包
        print("the urlStrOptional is \(urlStrOptional!)")
        print("the urlStr is \(urlStr!)")
        
        
    }

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


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值