iOS Question Get/Put doesn't seem to store Ture/False

Paul Leischow

Member
Licensed User
Longtime User
In B4A if you do this and everything works fine...
B4X:
    Dim ini As Map
    ini.Initialize
    ini.Put("test",True)
   
    Log(ini.Get("test"))

The result in the log file is: True


If you do the same in B4I the result is: 1

This is not good because if you add a Switch to the Page you get wrong results...
B4X:
Sub switch_ValueChanged (Value As Boolean)
    Log(switch.Value)
    ini.put("test",switch.Value)
    Log(ini.Get("test"))
End Sub

The Switch returns True or False ... Get/Put return 1 or 0 so you can't do this...
Switch.value=ini.Get("test")

Switch expects True or False, not 1 or 0
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This is not a bug. There is no real boolean type in iOS so boolean is stored as a number. However the number is correctly converted to a boolean when needed.

This code works:
B4X:
Dim ini As Map
ini.Initialize
ini.Put("test",True)

Switch1.Value = ini.Get("test")

Or better:
B4X:
Dim ini As Map = CreateMap("test": True)
Switch1.Value = ini.Get("test")
 
Upvote 0

Paul Leischow

Member
Licensed User
Longtime User
What am I doing wrong then?
The log results show 1,1,1,false,true
when it should read 1,1,1,true,true
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page

    Dim ini As Map
    Private switch1 As Switch
End Sub

Private Sub Application_Start (Nav As NavigationController)
    'SetDebugAutoFlushLogs(True) 'Uncomment if program crashes before all logs are printed.
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.Title = "Page 1"
    Page1.RootPanel.Color = Colors.White
   
    switch1.initialize("switch1")
    Page1.rootpanel.addview(switch1,20,20,100,20)
    NavControl.ShowPage(Page1)
    
   
    ini.Initialize
    ini.Put("test",True)
    Log(ini.Get("test"))
   
    File.writemap(File.DirDocuments,"test.dat",ini)   
    Log(ini.Get("test"))
   
    ini=File.readmap(File.DirDocuments,"test.dat")
    Log(ini.Get("test"))
   
    switch1.Value=ini.Get("test")
    Log(switch1.Value)

    switch1.Value=True
    Log(switch1.Value)
End Sub
 
Upvote 0

Paul Leischow

Member
Licensed User
Longtime User
So is it only B4I that has this issue with WriteMap where True/False is stored as 1/0 and it doesn't know how to convert it back?
Since it works fine with B4A... True/False is stored as True/False
 
Upvote 0

Paul Leischow

Member
Licensed User
Longtime User
Thanks Erel. It's easier to write for both platforms when you know and understand the differences and limitations of each :)
 
Upvote 0
Top