16649

In Swift, how can I use setValueForKey to set the textAlignment property of an object?

Question:

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?

Answer1:

You have to use raw value NSTextAlignment.Center.rawValue

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

Answer2:

Another option would be to declare a protocol with the properties you are interested in, and then extend the types you want to work with to adopt that protocol. IF that would work for you in this case, it seems safer.

Recommend

  • Vim configuration, setting up autocomplete, and columns
  • Handling browser-specific support of HTML input type=“date”
  • Image scaling geometry
  • C# - Why is Math.Atan(1) != anything near 45
  • Could be Text orientation of JTextArea changed by keyboard shortcut?
  • Zooming an element and its contents— an alternative to CSS3's zoom property?
  • Is it possible to specialize on a static lifetime?
  • UIButton Borders Function Only Gives Back White Borders
  • Quick Question About Get and Set
  • Responsive left sidebar open close
  • Put value at centre of bins for histogram
  • CSS - Cannot get one spanned style to override another inherited style and align left
  • redirect_to root_url and return unless @user.activated
  • Does Apportable support to build library binary (.a/.so)?
  • Salesforce Different WSDL files and when to use
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • How to use carriage return with multiple line?
  • How to modify the colors in the legend of a plot using a fill gradient?
  • Time complexity of a program which involves multiple variables
  • Switching to Release Build causes runtime error in Web Reference
  • Google Custom Search with transparent background
  • Checking free space on FTP server
  • Android fill_parent issue
  • Control modification in presentation layer
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • Adding custom controls to a full screen movie
  • jquery mobile loadPage not working
  • QuartzCore.framework for Mono Develop
  • GridView Sorting works once only
  • Confusion with PayPal's monthly billing cycle
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Django query for large number of relationships
  • Binding checkboxes to object values in AngularJs