3

I want all my views have a property stored like a tag, but it has to be type of String. So I tried using an extension for it.

But I got the error saying extensions my not contain stored properties.

The question is, how to store a string in each UIView in my app like a tag?

Thanks!

Tejas Patel
  • 850
  • 10
  • 26
Jacob Smith
  • 803
  • 5
  • 13
  • 23

5 Answers5

7

You can use the setAssociatedObject and getAssociatedObject defined in the objective c runtime to achieve this.

import ObjectiveC

var AssociatedObjectKey: UInt8 = 7
extension UIView
{
    var myOwnTag: String?
    {
        get
        {
            return getAssociatedObject(self, associativeKey: &AssociatedObjectKey) as? String ?? ""
        }

        set
        {
            var propertyVal : String? = nil
            if let value = newValue
            {
                propertyVal = value
            }
            setAssociatedObject(self, value: propertyVal, associativeKey: &AssociatedObjectKey, policy: objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

You can refer this blog for more details.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
1

You can't define categories (Swift extensions) with new storage; any additional properties must be computed rather than stored. In Swift, you'll need to define these yourself to get a computed property; something like:

extension String {
public var Foo : String {
    get
    {
        return "Foo"
    }

    set
    {
        // What do you want to do here?
    }
}
}
Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
0
class CustomView:UIView{
      var customTag:String?
    }
  1. You have to create a custom View, whose superclass is UIView.
  2. Add property of type string in this class.
  3. Change the type of your view to the customView which you have created by extending UIView.
Rohit Pradhan
  • 3,867
  • 1
  • 21
  • 29
  • I already use subclasses for different types of views. eg, MyCustomButton, MyCustomImageView and I didnt wanted to add stringTag parameter to each if I could avoid it. – Jacob Smith May 23 '16 at 07:20
0

You can create a class like this

class TagView: UIView {
    var stringTag: String = ""
}

and let your view to be this class.

Archie
  • 150
  • 6
-1

If all you are looking for is a view "tag" like but in type string, just use:

accessibilityIdentifier

Example programmatically:

myView.accessibilityIdentifier = @"YOYO";

Example via storyboard:

enter image description here

No extensions are needed, and also, later on you will be able to benefit from that if you will run UIAutomation tests on your app as UIAutomation use accessibilityIdentifier to identify views :)

Roy K
  • 3,319
  • 2
  • 27
  • 43
  • This is indeed easiest solution to problem at hand, but I would like to explore extensions as well. Thanks! – Jacob Smith May 23 '16 at 07:32
  • @JacobSmith, it is not only the easiest solution, it is also the recommended solution by Apple. But extensions is good to know, good luck! – Roy K May 23 '16 at 07:34
  • This is an abusive use of 'accessibilityIdentifier'. Extensions is the way to go. https://developer.apple.com/reference/uikit/uiaccessibilityidentification/1623132-accessibilityidentifier?language=objc – Dani Mar 17 '17 at 09:34
  • @DaniA disagree, what if he has 10 different types of views? Will you create an extension for each control class just to have another property that is already there for you? – Roy K Mar 17 '17 at 09:43