在Swift中,如何使用setValueForKey设置对象的textAlignment属性? [英] In Swift, how can I use setValueForKey to set the textAlignment property of an object?

查看:684
本文介绍了在Swift中,如何使用setValueForKey设置对象的textAlignment属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们定义一个这样的对象:

  let object:AnyObject = {

var result = UILabel(frame:CGRect(x:0,y:0,width:150,height:50))
result.text =First Text
result.backgroundColor = UIColor.blueColor )

返回结果
}()

,我明确地创建了一个 UILabel ,但在我的实际项目中,我不知道创建了什么类型的对象,所以我只是将结果作为 AnyObject 为此示例。对象只是一个标准的UILabel,蓝色背景上的文本为First Text,默认为左对齐。



在这个例子中,我知道这个对象实际上是一个 UILabel ,所以我知道我可以改变这样的属性:

 (object as UILabel).text =Second Text

现在我已经改变了文本。但是在我的实际项目中,我不知道我的对象是一个 UILabel ,所以我不能只做这个(对象作为UILabel) 窍门,因为它不总是一个 UILabel 。我的方法是使用 setValueForKey 。在使用 respondToSelector (本例中未显示)检查我的对象是否有一个名为text的成员后,我可以通过以下方式更改文本:

  object.setValue(Third Text,forKey:text)

现在,如果我想更改对象的 textAlignment ,检查该对象确实有一个名为 textAlignment ,我想做这样的事情:

  object.setValue (NSTextAlignment.Center,forKey:textAlignment)

这样做会导致错误: p>

 键入'NSTextAlignment'不符合协议'AnyObject'

我尝试使用这种方法,设置框架,因为它不喜欢 CGRect 类型,但是我可以通过将 CGRect 转换为 NSValu ë。但是,我找不到一种解决方案,用于枚举类型,如 NSTextAlignment



任何人都知道我可以如何实现我的目标?

解决方案

你必须使用原始值NSTextAlignment.Center.rawValue

  object.setValue(NSTextAlignment.Center.rawValue,forKey:textAlignment)


Let's say we define an object like this:

let object: AnyObject = {

    var result = UILabel(frame: CGRect(x: 0, y: 0, width: 150, height: 50))
    result.text = "First Text"
    result.backgroundColor = UIColor.blueColor()

    return result
}()

For this example, I explicitly created a UILabel, but in my actual project I don't know what type of object is created, so I'm just treating the result as AnyObject for this example. The object is just a standard UILabel with text of "First Text" on a blue background, default aligned to the left.

In this example case, I know that this object is actually a UILabel, so I know I can change it's properties like this:

(object as UILabel).text = "Second Text"

And now I've changed the text. But in my actual project, I don't know that my object is a UILabel, so I can't just do the (object as UILabel) trick, because it's not always a UILabel. My way around that is using setValueForKey. After checking that my object has a member called "text" using respondsToSelector (not shown in this example), I can change the text this way:

object.setValue("Third Text", forKey: "text")

Now, if I wanted to change the textAlignment of the object, after checking that the object does indeed have a member called textAlignment, I'd want to do something like this:

object.setValue(NSTextAlignment.Center, forKey: "textAlignment")

Doing this results in an error though:

Type 'NSTextAlignment' does not conform to protocol 'AnyObject'

I ran into something similar while trying this approach with setting the frame, because it didn't like the CGRect type, but I was able to get around it by converting the CGRect to an NSValue. However, I can't find a way to fix it for enum types like NSTextAlignment.

Anybody know how I can achieve my goal here?

解决方案

You have to use raw value NSTextAlignment.Center.rawValue

 object.setValue(NSTextAlignment.Center.rawValue, forKey: "textAlignment")

这篇关于在Swift中,如何使用setValueForKey设置对象的textAlignment属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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