BASIC Developer & Support Resources > Compilers

Apple Swift on Ubuntu

<< < (3/9) > >>

John:
Thanks AIR!

Great info on Swift C pointer declarations and use. I didn't notice anything about defining callback functions though. Any ideas on that topic?

AIR:
Just guessing here.  Using an IupButton callback as an example:


--- Code: ---int button_cb(Ihandle* ih)
--- End code ---

I think something like this?


--- Code: ---func button_cb(ih: UnsafeMutablePointer<Ihandle>) -> Int32
--- End code ---

As I mentioned, I'm just guessing...

John:
Looks good to me and your guesses have always been right on or got me pointed in the right direction.

I need to find out how to get the callback pointer to pass to IUP. From what I have stumbled on, this is available and core Swift syntax.

I hit the wall. I can't even get a UnsafeMutablePointer variable declared let alone a callback function. :-\


--- Code: Text ---import Foundationimport Glibc var dlg:UnsafeMutablePointer<Int32> = nilvar lbl:UnsafeMutablePointer<Int32> = nil IupOpen(nil, nil) lbl =  IupLabel("Hello world from IUP.")dlg = IupDialog(IupVbox(lbl, nil))IupSetAttribute(dlg, "TITLE", "Swift Hello World") IupShowXY(dlg, IUP_CENTER, IUP_CENTER) IupMainLoop() IupClose() 

jrs@laptop:~/Swift/usr/share/examples$ swiftc iuphw2.swift -import-objc-header /usr/include/iup/iup.h -liup -o iuphw2
iuphw2.swift:9:8: error: cannot assign value of type 'COpaquePointer' to type 'UnsafeMutablePointer<Int32>'
lbl =  IupLabel("Hello world from IUP.")
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
iuphw2.swift:10:25: error: cannot convert value of type 'UnsafeMutablePointer<Int32>' to expected argument type 'COpaquePointer'
dlg = IupDialog(IupVbox(lbl, nil))
                        ^~~
iuphw2.swift:11:17: error: cannot convert value of type 'UnsafeMutablePointer<Int32>' to expected argument type 'COpaquePointer'
IupSetAttribute(dlg, "TITLE", "Swift Hello World")
                ^~~
iuphw2.swift:13:11: error: cannot convert value of type 'UnsafeMutablePointer<Int32>' to expected argument type 'COpaquePointer'
IupShowXY(dlg, IUP_CENTER, IUP_CENTER)
          ^~~
jrs@laptop:~/Swift/usr/share/examples$

AIR:
two things you could try.

change the UnsafeMutablePointer to COpaquePointer

or ditch the explicit declaration of the variables and create them inline

var lbl =  IupLabel("Hello world from IUP.")

John:
Here is the results of your first idea. I'll try the other next. Thanks again for helping out!

AIR #1


--- Code: Text ---import Foundationimport Glibc var dlg: COpaquePointer<Int32> = nilvar lbl: COpaquePointer<Int32> = nil IupOpen(nil, nil) lbl =  IupLabel("Hello world from IUP.")dlg = IupDialog(IupVbox(lbl, nil))IupSetAttribute(dlg, "TITLE", "Swift Hello World") IupShowXY(dlg, IUP_CENTER, IUP_CENTER) IupMainLoop() IupClose() 

jrs@laptop:~/Swift/usr/share/examples$ swiftc iuphw2.swift -import-objc-header /usr/include/iup/iup.h -liup -o iuphw2
iuphw2.swift:4:10: error: cannot specialize non-generic type 'COpaquePointer'
var dlg: COpaquePointer<Int32> = nil
         ^
iuphw2.swift:5:10: error: cannot specialize non-generic type 'COpaquePointer'
var lbl: COpaquePointer<Int32> = nil
         ^
iuphw2.swift:10:30: error: nil is not compatible with expected argument type 'Any' (aka 'protocol<>')
dlg = IupDialog(IupVbox(lbl, nil))
                             ^
jrs@laptop:~/Swift/usr/share/examples$


AIR # 2

I think this is going to work. I just can't use variadic functions. Not supported yet.


--- Code: Text ---import Foundationimport Glibc IupOpen(nil, nil) var lbl =  IupLabel("Hello world from IUP.")var dlg = IupDialog(IupVbox(lbl, 0))IupSetAttribute(dlg, "TITLE", "Swift Hello World") IupShowXY(dlg, IUP_CENTER, IUP_CENTER) IupMainLoop() IupClose() 

jrs@laptop:~/Swift/usr/share/examples$ swiftc iuphw2.swift -import-objc-header /usr/include/iup/iup.h -liup -o iuphw2
iuphw2.swift:7:21: error: 'IupVbox' is unavailable: Variadic function is unavailable
var dlg = IupDialog(IupVbox(lbl, 0))
                    ^~~~~~~
__ObjC.IupVbox:2:13: note: 'IupVbox' has been explicitly marked unavailable here
public func IupVbox(child: COpaquePointer, _ varargs: Any...) -> COpaquePointer
            ^
jrs@laptop:~/Swift/usr/share/examples$

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version