如何在swift中的字典中插入值

如何在swift中的字典中插入值,swift,Swift,我先申报字典 let storeSentenceOrdering = [String, Int ](); 然后我想在其中插入值,但我得到一个错误: -我们可以用以下方法来完成 var carDictionary: [String:Int] = [:] // Below is a subscript way of adding or updating value to the Dictionary, if the key is not present then the key is inser

我先申报字典

let storeSentenceOrdering = [String, Int ]();
然后我想在其中插入值,但我得到一个错误:


-我们可以用以下方法来完成

var carDictionary: [String:Int] = [:]

// Below is a subscript way of adding or updating value to the Dictionary, if the key is not present then the key is inserted with the assigned value else its updated with the new value

carDictionary["Ford"] = 345000
carDictionary["Hyundai"] = 546788

print(carDictionary)
-我们还可以使用
update
为现有的
字典添加值

carDictionary.updateValue(234500,forKey:("Wolkswagen"))
-
字典
可以通过以下方式之一初始化

var carDictionaryOne: [String:Int] = [:]
var carDictionaryTwo: [String:Int] = ()
var carDictionaryThree = ["Ford":12345, "Audi":57689]

-我们可以用以下方法来完成

var carDictionary: [String:Int] = [:]

// Below is a subscript way of adding or updating value to the Dictionary, if the key is not present then the key is inserted with the assigned value else its updated with the new value

carDictionary["Ford"] = 345000
carDictionary["Hyundai"] = 546788

print(carDictionary)
-我们还可以使用
update
为现有的
字典添加值

carDictionary.updateValue(234500,forKey:("Wolkswagen"))
-
字典
可以通过以下方式之一初始化

var carDictionaryOne: [String:Int] = [:]
var carDictionaryTwo: [String:Int] = ()
var carDictionaryThree = ["Ford":12345, "Audi":57689]

-我们可以用以下方法来完成

var carDictionary: [String:Int] = [:]

// Below is a subscript way of adding or updating value to the Dictionary, if the key is not present then the key is inserted with the assigned value else its updated with the new value

carDictionary["Ford"] = 345000
carDictionary["Hyundai"] = 546788

print(carDictionary)
-我们还可以使用
update
为现有的
字典添加值

carDictionary.updateValue(234500,forKey:("Wolkswagen"))
-
字典
可以通过以下方式之一初始化

var carDictionaryOne: [String:Int] = [:]
var carDictionaryTwo: [String:Int] = ()
var carDictionaryThree = ["Ford":12345, "Audi":57689]

-我们可以用以下方法来完成

var carDictionary: [String:Int] = [:]

// Below is a subscript way of adding or updating value to the Dictionary, if the key is not present then the key is inserted with the assigned value else its updated with the new value

carDictionary["Ford"] = 345000
carDictionary["Hyundai"] = 546788

print(carDictionary)
-我们还可以使用
update
为现有的
字典添加值

carDictionary.updateValue(234500,forKey:("Wolkswagen"))
-
字典
可以通过以下方式之一初始化

var carDictionaryOne: [String:Int] = [:]
var carDictionaryTwo: [String:Int] = ()
var carDictionaryThree = ["Ford":12345, "Audi":57689]
试试看

      var dict : NSDictionary!
      dict = [1:"Hello", 2:"How", 3:"Are", 4:"You"]

      let value = dict[1] 
      print(value!)  // Print First Element

     //print all element

    for(var i=1; i<5; i++)
    {
        let value = dict[i]
        print(value)
    }
var dict:NSDictionary!
dict=[1:“你好”,2:“你好”,3:“你在吗”,4:“你”]
let value=dict[1]
打印(值!)//打印第一个元素
//打印所有元素
对于(var i=1;i尝试一下

      var dict : NSDictionary!
      dict = [1:"Hello", 2:"How", 3:"Are", 4:"You"]

      let value = dict[1] 
      print(value!)  // Print First Element

     //print all element

    for(var i=1; i<5; i++)
    {
        let value = dict[i]
        print(value)
    }
var dict:NSDictionary!
dict=[1:“你好”,2:“你好”,3:“你在吗”,4:“你”]
let value=dict[1]
打印(值!)//打印第一个元素
//打印所有元素
对于(var i=1;i尝试一下

      var dict : NSDictionary!
      dict = [1:"Hello", 2:"How", 3:"Are", 4:"You"]

      let value = dict[1] 
      print(value!)  // Print First Element

     //print all element

    for(var i=1; i<5; i++)
    {
        let value = dict[i]
        print(value)
    }
var dict:NSDictionary!
dict=[1:“你好”,2:“你好”,3:“你在吗”,4:“你”]
let value=dict[1]
打印(值!)//打印第一个元素
//打印所有元素
对于(var i=1;i尝试一下

      var dict : NSDictionary!
      dict = [1:"Hello", 2:"How", 3:"Are", 4:"You"]

      let value = dict[1] 
      print(value!)  // Print First Element

     //print all element

    for(var i=1; i<5; i++)
    {
        let value = dict[i]
        print(value)
    }
var dict:NSDictionary!
dict=[1:“你好”,2:“你好”,3:“你在吗”,4:“你”]
let value=dict[1]
打印(值!)//打印第一个元素
//打印所有元素

对于(var i=1;i)您是否尝试用谷歌搜索该问题?可能重复的只是错误的声明。更改为:let storeSentenceOrdering=[String:Int]()将let替换为var和good luckHave您是否尝试用谷歌搜索该问题?可能重复的只是错误的声明。更改为:let storeSentenceOrdering=[String:Int]()将let替换为var和good luckHave您尝试用谷歌搜索该问题了吗?可能重复的只是错误的声明。更改为:let storeSentenceOrdering=[String:Int]()将let替换为var和good luckHave您尝试用谷歌搜索该问题了吗?可能重复的只是错误的声明。更改为:let storeSentenceOrdering=[字符串:Int]()用var和good替换letluck@Uzair欢迎你,亲爱的…但是在stackoverflow.com,感谢通常通过放弃投票来表达…@Uzair你欢迎,亲爱的…但是在stackoverflow.com,感谢通常通过放弃投票来表达…@Uzair你欢迎,亲爱的…但是在stackoverflow.com,感谢通常通过给予来表达Upvots…@Uzair U是受欢迎的,亲爱的…但是在stackoverflow.com,感谢通常通过放弃投票来表达。。。。