Solution 1 :
You can set tag for view this way.
view.tag = 1
Solution 2 :
I may be late, but the actual answer given by @Muhammad Afzal is not exactly equivalent to .setTag()
in Android.
Swift .tag
only allows the use of Int
values, while Android .setTag()
allows the use of any Object
value.
To get an equivalent functionality, one should create a simple custom class like the following:
public class UITagView:UIView{
var Tag:Any?
init() {
super.init(frame: .zero)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setTag(_ tag:Any){
self.Tag = tag
}
func getTag()->Any{
return self.Tag!
}
}
If view
is an instance of the class, then view.setTag()
would be the the same in Android and Swift.
However, if the tags that you need are always Integers, it is easier to use view.tag
, as @Muhammad Afzal said.
Note: As in Android, when recovering an object with the .getTag()
method, it should be parsed to the class it belongs to (view.getTag() as! SomeClass
).
Problem :
I mean the method from the class View that allows to put a tag to an object of that class like this:
view.setTag(1)
Which would be the equivalent in Swift?