iOS Question Boolean handling within B4i

luke2012

Well-Known Member
Licensed User
Hi all,
within a B4XPages project I started to develop the app in the B4A (Android) version and now I'm checking the operation of the respective B4i (iOS) version.

Debugging the B4ì version I encountered a problem in the management of Boolean types.
Doing a search in the forum I realized that in iOS there is no real boolean type, so the following line of code worked correctly on the Android version (B4A) but not on the iOS version (B4i):

B4X:
dim published as string = "true" '--> I got this value from a JSON string field
dim IsPublished as boolean = published

if IsPublished then
    'Do something...
end if


So to get the check to work correctly i solved it like this:
B4X:
           dim IsPublished as Boolean

           #if B4A
                If published = "true" Then
                    IsPublished = True
                Else 
                    IsPublished = False
                End If
            #End If

            #if B4i
                If published = "1" Then
                    IsPublished = True
                Else 
                    IsPublished = False
                End If
            #End If
            
            If IsPublished = True Then
                 'Do something...
            end if

In your opinion is it a good solution to be adopted within all the modules of the B4XPages App or is it better to use another approach?

Thanks in advance for your suggestions :)
Luca.
 

Sandman

Well-Known Member
Licensed User
Not commenting on the strategy, just the code.

I'm not near a machine where I've got B4X available, so this isn't tested. It should give an idea on how to reduce conditionals and many lines to a single line working for both B4A and B4i.

B4X:
Public Sub convertToBoolean(value as String) As Boolean
    Return (value = "true") OR (value = "1")
End Sub
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Not commenting on the strategy, just the code.

I'm not near a machine where I've got B4X available, so this isn't tested. It should give an idea on how to reduce conditionals and many lines to a single line working for both B4A and B4i.

B4X:
Public Sub convertToBoolean(value as String) As Boolean
    Return (value = "true") OR (value = "1")
End Sub

It doesn't look good.

Can you upload the json string?

Hi Erel. Yes.
The value (boolean) I need to got is within "status" field:

B4X:
{"nid":[{"value":81}],"uuid":[{"value":"306da9f1-d85b-45e4-af92-c6afb2bf09e6"}],"vid":[{"value":954}],"langcode":[{"value":"it"}],"type":[{"target_id":"book_page_aziende","target_type":"node_type","target_uuid":"62b7260f-b1ae-47c5-83a7-da0ed5147397"}],"revision_timestamp":[{"value":"2021-10-05T08:32:20+00:00","format":"Y-m-d\\TH:i:sP"}],"revision_uid":[{"target_id":1,"target_type":"user","target_uuid":"e89c452a-3640-49e8-988f-0e8d46015573","url":"-"}],"revision_log":[],"status":[{"value":true}]}

So then I need to check it with: if status=true then then I did this:

B4X:
private published as boolean = status 'where status contains the JSON "status" value (that I got parsing the JSON string)

This statement work fine on B4A but not with B4i.
Within B4A if JSON "status" is "true" I see that the published (boolean type) is true.
Within B4A if JSON "status" is "true" I see that the published (boolean type) is false.
 
Last edited:
Upvote 0

Sandman

Well-Known Member
Licensed User
This json looks overly complicated. Did you produce it? Is everything really meant to be in arrays?

Also, try this tool to generate the code to parse the json: http://basic4ppc.com:51042/json/index.html

It will tell you what you need to do to get the value of status. That said, if you control the json, and there's no reason for the arrays, you could simplify it.
 
Upvote 0

Star-Dust

Expert
Licensed User
B4X:
Dim published as boolean = status 'where status contains the JSON "status" value (that I got parsing the JSON string)
Dim IsPublished as Boolean = ((published = "true") OR (published = "1"))
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
Tip: prettifying the string:
B4X:
Log(s.As(JSON).ToMap.As(JSON).ToString)

I wasn't able to reproduce it.
B4X:
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Dim s As String = $"{"nid":[{"value":81}],"uuid":[{"value":"306da9f1-d85b-45e4-af92-c6afb2bf09e6"}],"vid":[{"value":954}],"langcode":[{"value":"it"}],"type":[{"target_id":"book_page_aziende","target_type":"node_type","target_uuid":"62b7260f-b1ae-47c5-83a7-da0ed5147397"}],"revision_timestamp":[{"value":"2021-10-05T08:32:20+00:00","format":"Y-m-d\\TH:i:sP"}],"revision_uid":[{"target_id":1,"target_type":"user","target_uuid":"e89c452a-3640-49e8-988f-0e8d46015573","url":"-"}],"revision_log":[],"status":[{"value":true}]}"$
    Log(s.As(JSON).ToMap.As(JSON).ToString)
    Dim m As Map = s.As(JSON).ToMap
    Dim value As Boolean = m.Get("status").As(List).Get(0).As(Map).Get("value")
    If value Then
        Root.Color = xui.Color_Red
    End If
End Sub

Tested in debug and release modes.
 
Upvote 0
Top