Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] Android setTag equivalent in Swift

Posted on November 11, 2022 By

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?

READ  [FIXED] javascript - AsyncStorage setItem inside loop crashes
Powered by Inline Related Posts
Android Tags:android, swift, view

Post navigation

Previous Post: [FIXED] Is it possible to switch from Start state to End state in android MotionLayout using Java/Kotlin?
Next Post: [FIXED] android – PHP / SQL: cannot save image at server but successful save image link at SQL database

Related Posts

[FIXED] android studio – Kotlin, how to use ‘$’ as argument index in String.format Android
[FIXED] java – Sign up Button Crash Android
[FIXED] android – Can agora save the live streaming video and replay it inside the app.? Android
[FIXED] java – Simple loop for rolling dices n times Android
[FIXED] firebase – com.segment.analytics.android:analytics error while submitting my app to Google Play Console Android
[FIXED] android – Is it possible to save language option in DataStore? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme