首页 > 解决方案 > 如何清除我的字典数据。它存储数据并每次返回相同的消息

问题描述

此函数存储第一个值并返回相同的消息。

我试过这个:

FileManager.default.clearTmpDirectory()

       let resultt:NSDictionary = dict! as NSDictionary
            //let topup = resultt.value(forKeyPath: "Body.QR_TopupResponse.QR_TopupResult") as! String
            func getdata () -> String{
                guard let body = resultt["Body"] as? [String: Any] else {
                    // Failed to find body, return empty string
                    return ""
                }
                guard let response = body["QR_TopResponse"] as? [String: Any] else {
                    return ""
                }
                guard let result = response["QR_ToResult"] as? String else {
                    return ""
                }
                return result
            }
            print(getdata())

  self.presentAlert(withTitle: "", message: getdata())

 or  .. 
  var ftsm = resultsm.value(forKeyPath: 
"Body.QR_TOPResponse.QR_TOPResult") as! String

  // How to remove keypath.

标签: swiftdictionary

解决方案


要清除字典数据,您应该尝试:

myDictionary.removeAll()

或者您也可以尝试: removeAllObjects方法NSMutableDictionary源自 NSDictionary

[myDictionary removeAllObjects];
[myDictionary release];
myDictionary = [[NSMutableDictionary alloc] init];

因此,调用-removeAllObjects将清空字典并释放所有包含的对象。


推荐阅读