我对(例如)Info.plist
上的setValue:forKey
方法有疑问。
我有以下代码:
UIView
在调试器中,let label = UILabel()
label.layoutMargins = UIEdgeInsetsMake(10, 10, 10, 10)
let margins = label.valueForKey("layoutMargins")
的类型为margins
,其值为(AnyObject?)
。
如果我使用变量Some
在margins
上设置值,则按预期工作:
UIView
但是如果我尝试使用let label2 = UILabel()
label2.setValue(margins, forKey: "layoutMargins")
方法直接设置UIEdgeInsets结构,这当然不起作用,因为它需要setValue:forKey
而不是结构。
AnyObject
我是否可以将此label2.setValue(UIEdgeInsetsMake(5,5,5,5), forKey: "layoutMargins")
包装在某些内容中,或者将其转换为能够使用UIEdgeInsets
方法设置值的内容?
答案 0 :(得分:3)
您需要使用UIEdgeInsets
打包NSValue
。 iOS已通过+[NSValue valueWithUIEdgeInsets:]
提供必要的支持,因此解决方案应如下所示:
label2.setValue(NSValue(UIEdgeInsets: UIEdgeInsetsMake(5,5,5,5)), forKey: "layoutMargins")