Damn, it’s hard…
It’s hard to get used to the new IDE, new shortcuts, new programming language and the new OS at the same time. I feel like a kid who got lost in the woods. And it’s getting dark! Stay calm, don’t panic and try to find your way out…
I finished my App Development with Swift iBook and looked through Start Developing iOS Apps tutorial. Now that I have some basic knowledge it’s high time to get back to my Magic Hours project.
I started Xcode and opened the project I created while trying to set up a remote repository with git hub. I wanted to add a button to my view which should, when clicked, get user’s current location: latitude and longitude. This is a crucial information while estimating sunrise and sunset hours. I’ve read about actions and outlets in swift so I thought this would be pretty easy to achieve. But I have to admit I got lost in all those windows and files which were automatically included when I created the project. I used one of Xcode’s templates, a Paged-Based Application
and nothing worked as I expected. Xcode was giving me an error I didn’t understand, which in fact vanished after application restart (no comments). And as far as I can tell – there was some sample data included in the project which was useless for me so I came to the conclusion I need to step back and start with the very base project type, a single view application, as all tutorials do. I did delete my git hub repo again, created a new one, created a new iOS project, set up remote repository and was ready to put that button in my view.
My project structure in Xcode’s project navigator (like solution explorer in Visual Studio) looks now like this:
Main.storyboard is the view that is presented to the user after application launches. Generally speaking a storyboard is a visual representation of the app’s user interface. It’s like a .xaml in windows based applications. The ViewController.swift file on the other hand is like a .xaml.cs file, a code behind that can hold a reference to objects from the storyboard.
In order to place a button in my main view I have to drag and drop an object from the Object Library Window. And this is a thing that really bothers me. I don’t mind drag and drop, really, as long as there’s some other way to achieve the same result.
That’s a lot of dragging :) I’ve got only eleven inches of screen size, I work with my touch pad and still, I have to really focus in order to drop the element at the desired location.
Apple is really crazy about drag and drop and so in order to respond to button click event in code you have to, surprise!, do some more dragging. In windows phone / silverlight projects its enough if you double click from xaml designer or choose an item from combobox in properties tab. In Xcode / swift you create an action by ctrl clicking and item and dragging it to a proper view controller class. When this window pops up you can choose whether you want to create an action (subscribe to an event) or an outlet (which is in fact a property with a reference to a view from your storyboard).
I would say it’s not an ergonomic solution, it’s a nuisance. A simple double click would be so much better…
Now that I have my button and I’m able to respond to the clicked event, it’s time to copypaste some code from stack overflow ;) I want to get latitude and longitude of user’s current location. After struggling with different approaches I was finally able to put together a working example. Bad news is that simple copypasting is sometimes not enough as some of the upvoted examples are meant for the previous versions of swift. The code does compile, there are no errors, but you don’t get the expected results :)
In the end I got what I wanted, so this is like a first step in my iOS developer career ;)