Swift 在字典中添加nsindepath作为键和NSNumber作为值时出现问题?

Swift 在字典中添加nsindepath作为键和NSNumber作为值时出现问题?,swift,dictionary,boolean,nsnumber,nsindexpath,Swift,Dictionary,Boolean,Nsnumber,Nsindexpath,self.preferencesListFilterDictionary中quickFilterDictionary的第二个对象是nil。如何在字典中添加[nsindepath]索引路径(作为键)和NSNumber(作为值) 注意:第一个对象添加成功,当我在控制台中打印self.preferencesListFilterDictionary时,只有第二个对象是nil。请问,我在下面的代码中做错了什么 self.preferencesListFilterDictionary["price"] =

self.preferencesListFilterDictionary中
quickFilterDictionary
的第二个对象是
nil
。如何在
字典中添加[
nsindepath
]索引路径(作为键)和
NSNumber
(作为值)

注意:第一个对象添加成功,当我在控制台中打印
self.preferencesListFilterDictionary
时,只有第二个对象是
nil
。请问,我在下面的代码中做错了什么

self.preferencesListFilterDictionary["price"] = 1000
self.quickFilterArr = [localizedStrOf("deliveryNowOnly"),localizedStrOf("rated")]
var quickFilterDictionary = Dictionary<NSIndexPath, NSNumber?>()
        for obj in self.quickFilterArr {
            let row = self.quickFilterArr.indexOf(obj)
            let indexPath = NSIndexPath(forRow:row!, inSection: 0)
            quickFilterDictionary[indexPath] = NSNumber(bool: false)
        }
        print(quickFilterArr.count)
        print(quickFilterDictionary)
self.preferencesListFilterDictionary.updateValue(quickFilterDictionary as? AnyObject, forKey: "quickFilterDictionaryKey")
print(self.preferencesListFilterDictionary)
为了从数字中获取字典布尔值,我编写了以下代码-

func getQuickFilterValueOfIndexpath(indexPath:NSIndexPath) -> Bool {
        if let val = self.preferencesListFilterDictionary["quickFilterDictionaryKey"] {
            // now val is not nil and the Optional has been unwrapped, so use it
            let tempDict = val as! Dictionary<NSIndexPath, NSNumber?>
            if let boolVal = tempDict[indexPath] {
                return boolVal!
            }
        }

        return false
    }
func getQuickFilterValueOfIndexpath(indexPath:NSIndexPath)->Bool{
如果let val=self.preferencesListFilterDictionary[“QuickFilterDictionary”]{
//现在val不是nil,可选值已展开,因此请使用它
让tempDict=val作为字典
如果让boolVal=tempDict[indexPath]{
返回布尔瓦尔!
}
}
返回错误
}

我认为您对行、IndexPath和索引之间的区别感到困惑

在以下代码中:

let row = self.quickFilterArr.indexOf(obj)
如果您选择单击行,您将看到它是一个类型为
Array.Index?
的对象。这不是一场争吵

您使用NSNumber、NSIndexPath和update值作为密钥还有什么原因吗?与数字相反,IndexPath只需使用以下内容更新字典值: preferencesListFilterDictionary[“quickFilterDictionary]=quickFilterDictionary

如果您选择单击quickFilterArr,它是一个IndexPath数组[IndexPath]——要在该数组中所选索引处引用IndexPath,您只需执行以下操作:

if let arrayIndex = quickFilterArr.indexOf(obj) {
//if your array is an array of Index Paths
//you would then pull the IndexPath object at the index you have defined above
  let indexPath = quickFilterArr[arrayIndex]
  quickFilterDictionary[indexPath] = Number(bool: false)
}

我认为您对行、IndexPath和索引之间的区别感到困惑

在以下代码中:

let row = self.quickFilterArr.indexOf(obj)
如果您选择单击行,您将看到它是类型为
Array.Index?
的对象。它不是行

另外,您使用NSNumber、NSIndexPath和update value作为键是否有原因?与使用Number、IndexPath和仅使用以下内容更新字典值不同: preferencesListFilterDictionary[“quickFilterDictionary]=quickFilterDictionary

如果您选择单击quickFilterArr,它是一个IndexPath数组[IndexPath]——要在该数组中所选索引处引用IndexPath,您只需执行以下操作:

if let arrayIndex = quickFilterArr.indexOf(obj) {
//if your array is an array of Index Paths
//you would then pull the IndexPath object at the index you have defined above
  let indexPath = quickFilterArr[arrayIndex]
  quickFilterDictionary[indexPath] = Number(bool: false)
}

你好@Kayla Galway,你错过了这句话-self.quickFilterArr=[localizedStrOf(“DeliveryNow”)、localizedStrOf(“rated”)]你好@Kayla Galway,你错过了这句话-self.quickFilterArr=[localizedStrOf(“DeliveryNow”)、localizedStrOf(“rated”)]