2015-12-16 89 views
-2
{ 
ac = 0; 
storeLocation = "<null>"; 
storeSizeSqFt = 1000; 
tinNumber = testdummy4y58; 
wirelessInternet = 0; 
} 

访问数据我已经写代码JSON解析和字典

func StoreInfo(notification : NSNotification){ 
     if let result = notification.userInfo{ 
      print(result) 
      if let particularStoreInfo = result["data"] as? NSDictionary{ 
       print(particularStoreInfo) 
       if let temp = particularStoreInfo["store"] as? NSDictionary{ 
         print(temp) 
       } 
      } 
     } 
    } 

我已经成功地走过的字典里,但现在指导我如何保存和存储字典里访问的细节。

回答

1

要访问NSDictionary中的数据,您可以使用此方法,假设temp在您的问题是您要从中检索信息的字典。

temp.objectForKey("ac") // Key can be of type AnyObject 

以上将检索钥匙“ac”下保存的信息。

要将数据存储在NSDictionary中,必须使用NSMutableDictionary代替,其中包含方法setObject

的保存信息的示例:

var value = "This is the string I will save in the dictionary" 
var dictionary = NSMutableDictionary() 
dictionary.setObject(value, forKey: "Name of the Key") 

更新:更改setValue例如setObject。有关详情,请参阅@ vadian的评论。

+2

除非您确实需要KVC功能,否则请勿使用'valueForKey:'/'setValue:forKey:'。 – vadian