Swift Type Casting

Swift is a Type Safe language. That means that it is obsessed with what type of data is associated with what

When we declare a variable or constant, there are a few ways we can do it:

These lines essentially do the same thing (with the exception of the constant name and the actual number clearly). The extra syntax in the second example is simply declaring the type of data that ‘anotherNumber’ is going to be an Int value. One of the most useful bits of advice I got when I started learning swift is to read the colon as “is of type”. To that line would read as “The constant anotherNumber is of type Int and equals 8”.

That being said the preferred way is to leave the type declaration off and let swift infer the type itself. In this example its blatantly clear the ‘anotherNumber’ is an integer, so there is no need for the explicit type declaration.

Type safety is awesome! Why? Well it simply means that once a value is set we know what type of data it will contain. That means that A String will always be a String, never an Int or any other type of data.

 

 

Swift Variables & Constants

Variables

To create a variable in swift, you start with the keyword ‘var’, followed by your name (camel cased, because you’re a good developer) and then a value:

Variables, as the name implies, can change over time. For example at a later point you could reassign the ‘someWords’ value:

Constants

Constants are declared very similarly to variables, except instead of ‘var’, you use ‘let’:

Again, as the name suggests, constants cannot change. Once they are set, they keep the value until they are deallocated.

Notes

So why would you ever use a constant, they’re like variables, but with restrictions, right? Yes, but there are two main reasons. Most importantly, it makes your intent very clear. By making something a constant, you are explicitly stating that this value will not change, ever. By using a variable, you are stating that, yes, this may well change when I use it again at a later point in time. Secondly, the compiler can make some slight optimisations for constantly.

Xcode will actually inform you if you have declared a variable and never changes it, and suggest you change it to a constant, and if you try to change a constant it force you to change it’s declaration to a variable.

Tip

Basically, just declare everything with ‘let’. When you try and change it Xcode will force you to change the declaration.

 

Setting Up A Firebase Project in Xcode

This short guide will go over the few extra steps needed to get your Xcode project up and running with Firebase. This guide follows on from a previous post that demonstrates how to install the Firebase libraries with CocoaPods.

You need to create a new Firebase account, the free plan aka ‘Spark’ is more than enough to get you up and running.

When you login you’ll be presented with an empty Firebase console, click “Create New Project”

 

 

Generally each of your apps will be associated with a different Firebase project. It makes sense to name them appropriately, but this doesn’t matter too much (it can also be changed at a later date).

 

 

The next part is important. You need to enter the bundle ID for the app your Firebase project is going to be linked to. You can find this by going to the ‘General’ tab in your app settings.

 

 

The next screen will present you with a link to download a ‘GoogleService-Info.plist’ file. Download this and drag it into your project navigator to add it to your project.

 

 

The rest of these steps discuss how to install the Firebase libraries using Cocoapods, a guide on how to do that can be found here.

That’s it, you should now have project ready to start using Firebase.

Media Events jQuery

A common thing to want to do is respond to the state of some media (audio of video) playing on your page. You can tie into a number of events on the media object, like when the video is playing, when it is paused, even find out the current time of the video.

However, we’ll start by looking at probably the most common scenario, which is to do something when a video (or audio) comes to a stop, the is the “ended” event.

We’ll first set up a simple video in our HTML. Most importantly, we need to give it an id attribute. Here, I’ve called it “myVideo”:

 

Note: As always we’ll be working inside our $(document).ready() runction. 

 

Installing CakePHP 3 On Digital Ocean Ubuntu 14.04

Summary

  • How to install  a CakePHP 3 app on a new droplet running Ubuntu 14.04.

This guide assumes you’ve set up a Ubuntu 14.04 server and have MYSQL up and running. This guide uses the “PHPMyAdmin” from the “One Click Apps” available on Digital Ocean.

 

Installing Composer

First, update you package manager:

Install cURL:

You’ll be asked if you want to use the extra space, type ‘yes’ and hit enter.

Finally, install composer:

You should now be able to just type ‘composer’ and see the list of available commands.

Enable the rewrite module

Make sure the rewrite module is enabled

Restart the server:

Install the intl extension:

Restart the server:

Install Cake

This is all the requirements for CakePHP to run. We are now ready to use composer to download and setup our application. First off we need to change to the directory that we want it installed to. This will most likely be your public folder:

You can now use composer to setup your project, giving your application some kind of relevant name:

You’ll be asked if you would like to set the folder permissions, type “Y” and hit enter.

Enable AllowOverride

Navigate to the “/etc/apache2” directory, download and open the “apache2.conf” file. Around line 166 within the “<Directory /var/www/>” tags, change the that says “AllowOverride None” to:

You’ll need to restart the server for the change to take effect:

Update The Web Root For Our Droplet

At the moment when we visit our droplet via a browser, we are taken to the “html” folder. If you look in the CakePHP there is a “webroot” folder. This is actually the folder we need to point our droplet to. We can do this in one of the apache config files.

Navigate to the ‘/etc/apache2/sites-enabled’ directory and download and open the ‘000-default.conf’ file. You need to just change one line (around line 12):

Upload the modified file, replacing the old one. You’ll need to restart the server for the settings to take effect:

You should now be able to visit your droplet IP and see the default CakePHP page.

Debug Kit

Debug kit (the very useful debug toolbar that CakePHP comes with) is turned on by default in this initial installation, however,  it requires sqlite to be installed on our server for it to work.

To install sqlite run these few commands:

And finally restart the server one last time:

 

 

Installing Composer On Digital Ocean Ubuntu 14.04

Summary

  • How to install composer on a new droplet running Ubuntu 14.04.

Installing composer on a Digital Ocean droplet is a pretty straight forward process and only needs a few commands. Log in to your droplet.

First, update you package manager:

Install cURL:

You’ll be asked if you want to use the extra space, type ‘yes’ and hit enter.

Finally, install composer:

You should now be able to just type ‘composer’ and see the list of available commands.

Log in To Digital Ocean Droplet Ubuntu 14.04

Summary

  • How to log in and change the default password of a new droplet running Ubuntu 14.04.
  • Finding the default MYSQL password.

You will have been emailed your droplet details, most importantly a password (which you will need to change in a moment), so have this email open.

Open a new terminal window and log in to your droplet:

You will be asked if you want to continue, type ‘yes’ and hit enter.

On your first log in you will be required to change the default password emailed to you. First, enter the default password:

Then your new password twice:

Note: These are secure fields, so it won’t look like you’re typing anything. 

If you scroll up slightly, you should be able to find you MYSQL password output somewhere in the text, take note of this:

You should be able to go to your droplet IP in a browser and see the default apache page.

You should be able to go to http://YOUR_DROPLET_IP/phpmyadmin and see the familiar looking phpMyAdmin login screen. You can log in with the username of ‘root’ and the password you took note of in the above step.

Login With FileZilla

You should now be able to login with an FTP program as well. The host is the droplet IP, prefixed with ‘sftp://’ username and password are the same as above (with whatever password you set up).

By default, you should be taken to a folder called ‘root’. Come out of this folder by clicking the top icon in the directory (the two dots). The public directory is the /var/www/html folder. In there you’ll see the default apache html file and an info php file. Feel free to delete both of this if you want. This is the folder where you should upload you website files.

Digital Ocean WordPress Permalinks

Summary

  • How to fix permalink issues on a Ubuntu 14.04 Digital Ocean server

Setting up custom permalink on a Digital Ocean server is something that I’ve had to do time and time again. And each time I come to do it, I’ve forgotten how. So I’ve decided to document it, mostly for my own benefit to stop me losing my mind when I inevitably come to do it 6 months from now, but also in the hope it will help out someone else.

So perquisites:

  • Firstly, this is not about how to set up MySQL, phpMyAdmin etc.
  • Nor is it about how to install WordPress from stat to finish, only about setting up custom permalinks.
  • This is on a clean Ubuntu 14.04 x64 server, not going down the auto setup option for wordpress that Digital Ocean offers. I would assume that image would come already configured appropriately, but haven’t tried it myself.
  • This should work with both the ip url or a custom domain url.

Firstly make sure the root directory of your install (where your wp-config.php file lives) contains a .htaccess file. . The contents of the file should look like this:

If for some reason it doesn’t, you can just create a new file in your chosen script editor and save it as ‘.htaccess’.

You need to enable the server rewrite module. To do this log into your droplet in the terminal and run the following command:

You need to make a small change to the apache2.conf file. You can do this via the terminal or logging into the server with an FTP client editing the file with a text editor. Either way the file you need is located in the /etc/apache2/ directory and called apache2.conf.

Full path: /etc/apache2/apache2.conf

You need to make a subtle but important change. Locate the block of code that looks like this:

Make sure you’re in the correct code block as there are three that look similar, make sure it starts with ‘Directory /var/www’ as this is the public root of the server. In my case the block started at line 164.

Change the AllowOverride line to:

Finally, any changes made to the server servings require a server restart in order to take affect. Do this with this command:

With that done, your “pretty urls” should now work. One final note is sometimes its worth going into the permalink setting and hitting save, even if its just reapplying the same settings.

References