Here you can find a new (experimental) tool called Swifter (v. 1.0 beta) which allows you to add Swift code to your B4i Project.
How to use?
Important notes:
Feel free to ask questions
How to use?
- Open Swifter and enter the B4i installation path
- enter the path of the project in which you want to use Swift
- Click on Activate and restart B4i (if it was open)
- In every project, you want to use Swift, you need this code snippet:
B4X:
Private Swifter As NativeObject
Sub CallSwift(MethodName As String, ParamNames() As List, Params() As Object) As NativeObject
If Not(Swifter.IsInitialized) Then
Swifter = Me
Swifter = Swifter.RunMethod("InitSwifter",Null)
End If
If Not(ParamNames = Null) Then
Dim ArgsStr As String
For Each Arg As String In ParamNames
If ArgsStr = "" Then ArgsStr ="With"
If Arg.Length > 1 Then
ArgsStr = ArgsStr & Arg.SubString2(0,1).ToUpperCase & Arg.SubString(1) & ":"
Else
ArgsStr = ArgsStr & Arg.ToUpperCase & ":"
End If
Next
End If
Return Swifter.RunMethod(MethodName & ArgsStr,Params)
End Sub
#if objc
#import "B4iProject-Swift.h"
-(Swifter*)InitSwifter{
Swifter* SW = [[Swifter alloc] init];
[SW setSwifterWithB: self.bi];
return SW;
}
#End If
- so just copy the snippet to your project
- now you can add you swift code (inside a condition block):
B4X:
#if swift
func GetString() -> NSString{
return "Your text"
}
#End If
- Call the functions with the CallSwift method, in this case:
B4X:
Dim GetString As String = CallSwift("GetString",Null,Null).AsString
Important notes:
- Because of this problem, you will need to comment the "TAB" property line in the iCore.h file (you will find the file in the Libs folder of your builder server). --> Workaround for the missing TAB property:
B4X:
Dim TABU As String = Chr(9)
- This means also that hosted builder users can't use Swifter for now as they don't have access to this file.
- After you have edited your Swift code it's not enough to just restart you App, you need to rebuild your App completely (same behaviour like inline Objective C code)
- Everytime you activate or deactivate Swifter you have to restart B4i
- You shouldn't work on two B4i projects at the same time with an activated Swifter
- when working on your Swift project, keep Swifter open
- use Swift code only in the Main module
- administrator rights are required for editing the Xcode template
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
Private Swifter As NativeObject
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
NavControl.ShowPage(Page1)
Dim GetString As String = CallSwift("GetString",Null,Null).AsString
Log(GetString)
CallSwift("SetPageColor",Array("Col","Page"),Array(Swifter.ColorToUIColor(Colors.Magenta),Page1.RootPanel))
CallSwift("RaiseEvent",Null,Null)
End Sub
Sub RaiseMe (Info As String)
Log("Info: " & Info)
End Sub
Sub CallSwift(MethodName As String, ParamNames() As List, Params() As Object) As NativeObject
If Not(Swifter.IsInitialized) Then
Swifter = Me
Swifter = Swifter.RunMethod("InitSwifter",Null)
End If
If Not(ParamNames = Null) Then
Dim ArgsStr As String
For Each Arg As String In ParamNames
If ArgsStr = "" Then ArgsStr ="With"
If Arg.Length > 1 Then
ArgsStr = ArgsStr & Arg.SubString2(0,1).ToUpperCase & Arg.SubString(1) & ":"
Else
ArgsStr = ArgsStr & Arg.ToUpperCase & ":"
End If
Next
End If
Return Swifter.RunMethod(MethodName & ArgsStr,Params)
End Sub
#if objc
#import "B4iProject-Swift.h"
-(Swifter*)InitSwifter{
Swifter* SW = [[Swifter alloc] init];
[SW setSwifterWithB: self.bi];
return SW;
}
#End If
#if swift
func GetString() -> NSString{
return "Your text"
}
func SetPageColor (Col: UIColor, Page: UIView){
Page.backgroundColor = Col
}
func RaiseEvent() {
bi!.raiseEvent(nil, event: "raiseme:", params: ["very important"])
}
#End If
Feel free to ask questions
Last edited: