为什么我不能将 SetValue 用于字典? [英] Why I can not use SetValue for Dictionary?

查看:36
本文介绍了为什么我不能将 SetValue 用于字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是 swift 新手,在我的应用程序中,我声明了一个这样的字典:

Hello Everyone I am new in swift and In my app I declare a dictionary like this :

var imageDict : Dictionary<String, String> = [:]

我想像这样为该字典设置值:

and I want to set values for that dictionary like this :

imageDict.setValue(NSStringFromCGPoint(frame), forKey: NSString.stringWithString("/(tagValue)"));

但是我得到了这样的错误:

But I got error like this :

Dictonary <String, String> does not have a member named 'setValue'

这个问题来自我之前的问题,任何人都可以解释我为什么不能为该字典设置值,并且任何人都可以告诉我任何其他方法吗?

This question is from my previous question and can enybody explain my why I can not set value for that dictionar and can enybody tell me any other way to do that?

提前致谢.

推荐答案

Swift 字典没有像 setValue:forKey: 这样的方法.只有 NSMutableDictionary 有这样的方法.如果你想在 swift 中为 key 赋值,你应该使用下标.如果您想使用 swift 字典来做,这是正确的方法.

Swift dictionary does not have method like setValue:forKey:. Only NSMutableDictionary has such methods. If you wish to assign value for key in swift, you should use subscripting. Here is the correct way to do it, if you wish to do it with swift dictionary.

var imageDict:[String: String] = [:]

imageDict["\(tagValue)"] = NSStringFromCGRect(frame)

或者如果你想使用 NSMutableDictionary,它看起来像这样,

Or if you wish to use NSMutableDictionary then, it looks like this,

var imageDict = NSMutableDictionary()
imageDict.setObject(NSStringFromCGRect(frame), forKey: "\(tagValue)")

这篇关于为什么我不能将 SetValue 用于字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆