Wish Missing Function MAP

Discussion in 'iOS Bugs & Wishlist' started by mcGeorge, Nov 15, 2014.

  1. mcGeorge

    mcGeorge Member Licensed User

    Hi, i often use the methods

    "Map.GetKeyAt()" and "Map.GetValueAt()"

    in my B4A-Programms.

    Is it possible to implement these methods in B4I?

    Thanks, George
  2. billzhan

    billzhan Active Member Licensed User

  3. Erel

    Erel Administrator Staff Member Licensed User

    These methods will not be added.

    The only purpose of these methods in B4A was to allow developers to iterate over the Map items. This was before the For Each loop was available.
    A standard Map structure doesn't support these methods so adding them requires a more complicated (and less optimized) structure.

    For Each k As String In Map.Keys
    Dim v As Object = Map.Get(k)
    This code is faster and doesn't require a custom structure under the hood.
  4. jcesar

    jcesar Active Member Licensed User

    But if i need recover a key for some value ?
  5. LucaMs

    LucaMs Expert Licensed User

    Ok, but please, do not remove them from B4A ;)
  6. Erel

    Erel Administrator Staff Member Licensed User

    They will not be removed.

    Hash maps are not suitable for this task. It is better to use two maps in this case.

    This task requires linear search:
    Sub FindKeyForValue(m As Map, v As Object) As Object
    For Each k As Object in m.Keys
    If m.Get(k) = v Then Return k
    End Sub
    jcesar likes this.
  7. tamayo461

    tamayo461 Member Licensed User

    You managed to implement the method in iOs?
  8. Erel

    Erel Administrator Staff Member Licensed User

    These methods are not available in B4i. You should instead use For Each to iterate over the items.
  9. JonPM

    JonPM Well-Known Member Licensed User

    The reasoning is logical, however there is one profound reason why you should implement these methods: to maintain the promise of a cross-platform suite. Migrating large projects from B4A to B4i is mostly unobtrusive, however these sort of inconsistencies slow the process.
  10. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible to add these methods without a building a completely new collection (with worse performance, more overhead and less flexibility). As these methods are deprecated and there is a better alternative I decided that they will not be implemented.
  11. Bryanne Vega

    Bryanne Vega Member Licensed User

    I worked around this issue in the TableView by passing my data to each cell's tag.

    Sadly I cant either port directly non UI code because of this.
  12. iCAB

    iCAB Well-Known Member Licensed User

    Can I trust, the loop below to return the keys in the same order as if I used I used B4A, "Map.GetKeyAt(i)" and "Map.GetValueAt(i)" .

    For Each k As String In Map.Keys
    Dim v As Object = Map.Get(k)

    In other words

    Dim iLoopIndex As Int = 0
    Dim iKeyIndex As Int = 5

    For Each k As String In mLocalMap.Keys
    Dim v As Object = mLocalMap.Get(k)
    If iLoopIndex = iKeyIndex Then
    End If
         iLoopIndex = iLoopIndex + 
    Is k = GetKeyAt(5) ??? assuming that map size is > 6
  13. Erel

    Erel Administrator Staff Member Licensed User

    No. The order is not preserved.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice