美文网首页
Dictionary字典

Dictionary字典

作者: 架构师的一小步 | 来源:发表于2018-12-05 17:58 被阅读0次

初始化

        //初始化方式1
        let dic=["productid":323129]
        //初始化方式2  <String,Int> key  (string类型)  value (Int类型)
        let dic1:Dictionary<String,Int>=["productid":323129]
        //初始化方式3
        let dic2:[String:Int]=["productid":323232]
        //初始化方式4
        let empty=Dictionary<String,Int>()
        //初始化方式5
        let empty1=[String:Int]()

获取值

   let id=dic["productid"]  //323129
   //dic没有给key为productid1设置值,所以会出现空
   let id=dic["productid1"] //nil

修改值

        var dic=["pro":323129]
        //将key为pro的值改为43345
        dic["pro"]=43345
        //修改key为pro的值变成22333,返回开始的值43345
        let oldId=dic.updateValue(22333, forKey: "pro")

删除值

        var dic=["pro":323129]
        //删除key为pro的value值
//        dic["pro"]=nil
        //如果这个key原来存在的话会返回这个key原来所对应的value
        let oldId=dic.removeValue(forKey: "pro1")
        //判断dic是否为空
        if dic.isEmpty
        {
            print("dic is empty")
        }

获取网址的key,value值

let url="http://www.kaola.com/?navindex=1&zn=top"
/**
 *第一种方式
 */
//获取url额components
let urlComponent=NSURLComponents(string: url)!
//取出components中的queryItems的key value
for queryItem in urlComponent.queryItems!{
    print(queryItem.name)
    print(queryItem.value)
}

/**
 *第二种方式
 */

//获取?的位置
let range=url.range(of: "?")!

//获取url在range和url.endIndex的值
let queryString=url[url.index(after: range.lowerBound)..<url.endIndex]
//对带&的进行拆分
let componts=queryString.components(separatedBy: "&")
//空字典
var parameters=[String:String]()
//循环遍历将值存到字典中
for component in componts{
    //对值进行二次拆分通过=号
   let keyValue=component.components(separatedBy: "=")
    parameters[keyValue[0]]=keyValue[1];
}

相关文章

  • Swift第二篇(字典&集合)

    Swift字典:Dictionary Swift中的字典Dictionary与Foundation中的NSDict...

  • 字典&&列表&&元组

    {字典}&&[列表]&&(元组) 字典(dictionary) 创建dictionary:每个元素都是一个key-...

  • Python 字典(Dictionary)(1)

    Python 字典(Dictionary) 访问字典里的值

  • 字典-Dictionary

    这里要讲到的字典也是一种数据类型,你别理解成新华字典或者成语字典就Ok了,它其实是能够存储任何数据类型的对象......

  • 字典dictionary

    字典(dictionary) 字典可看作是键(key)与值(value)之间的一系列一一对应关系。 字典和列表相似...

  • dictionary字典

    获取字典指定key的值 dict.get(key[, default])VSdict[key] dict.get(...

  • Dictionary字典

    初始化 获取值 修改值 删除值 获取网址的key,value值

  • 字典(Dictionary)

    本文学习参考:http://www.runoob.com/python/python-dictionary.htm...

  • Swift字典

    ``` // 字典格式 // 字典名称 = {字典关键字}() var emptyDic = Dictionary...

  • YYModel 之个人Tips

    1.如何判断字典为空 if (!dictionary || dictionary == (id)kCFNull) ...

网友评论

      本文标题:Dictionary字典

      本文链接:https://www.haomeiwen.com/subject/nmdwcqtx.html