Things got much more tricky when I started trying to implement SimpleShare, my Bluetooth LE (CoreBluetooth) mesh network sharing app, in Swift, using the new Multipeer Connectivity framework instead of Bluetooth. There were much more complicated things I needed to do in Obj-C, and the app couldn’t be “translated” in the same way as the simpler apps just by changing to the new syntax, since Swift actually works differently in key ways than Obj-C does.
Things I learned about Swift:
- Optional variables: You can no longer have variables that aren’t set or that are nil like you could in Obj-C. If you don’t know what a variable is when the class is created, you have to make it an optional variable by putting a
?
after the type declaration.
1 2 3 4 |
|
Global variables: It seems that any variables you set within a class but outside of a function act like public parameters for that class and are accessible to read or set from other classes
Calling functions from another class: You call a function from another class by writing the class name followed by a dot then the function name with its arguments, eg:
MyClass.someFunction(arg1,arg2)
Init functions are a special case: Instead of calling
MyClass.init(arg1,arg2)
like you would for any other function, just callingMyClass(arg1,arg2)
is how you can init an instance of a class.Convenience init: If your init function requires arguments to create a class instance, you can create a second init method for convenience that creates an instance with default values.
1 2 3 4 5 6 7 8 9 |
|
In the afternoon, I made a plan with Jorge and Kyle to work on a mesh networking inspector to figure out the limitations of Multipeer Connectivity so that we can build a stable mesh networking messaging app. We decided to move forward on the project in Objective-C while continuing to learn Swift on the side so we can finish more quickly.