Category Archives: macOS Development

How to fix missing file warnings in Xcode 8

Xcode 8 has this annoying habit to show missing files as warnings. This is happening when we delete a file that is referenced by a project using Finder rather than remove it using Xcode.

Technically it’s the git version control that complains about the missing files, not Xcode. However, since git says “yo, there’s a conflict between what should be and what is”, Xcode tells us this as a warning.

Be that as it may, how do we fix it before going insane? Lucky for us, it’s easy to fix. Here’s how.

Open a Terminal session and cd into your project directory. In here, simply type

As soon as you press Enter and return to Xcode, all those nasty warnings are gone. What we’ve done here is to say to git, “listen, this is the new state of the directory, please ignore what you think it should be”.

There should be no response from git, which is good news. All we see is no more missing file warnings in Xcode, and appropriate A and D icons in front of new and removed files as a result.

How to create infinite loops in Objective-C and C

Infinite loops can be useful to execute some code until an exit signal is given. For example, a command line menu could be waiting forever until the user makes a valid choice. There are other approaches of course, but in case you need to know the syntax, here it is for all three loops.

Infinite for loop

Infinite while loop

Infinite do-while loop

To use the latter variations in C, replace the TRUE condition with 1.

How to add a macOS target to your iOS App

To write cross-platform applications, it can be beneficial to have a single project with several target architectures. For example, we may want a macOS App inside a project that started out as iOS, and vice versa. Or we may want a different version of our app, perhaps a free one with less features, and an expensive one with more, based on the same code.

That’s where Xcode Targets come in. A Target is something that defines several build settings about an app so that when we press that popular button in Xcode, it knows what to do so we can see the built app in full colour. Trust me, there’s a lot going on under the hood – if you’ve ever tried to compile from the command line, you know how super helpful that button is. But I digress…

In this example I’ll show you how we can add a macOS Target to an iOS App’s Project. This will allow us to run and build either an iOS or a macOS version from common code.

Let’s begin. I’m using Xcode 8.3.3 for this by the way.

Adding the Target

In a standard Xcode Project for iOS, we already have a single target. Click on the blue project bar and select it from the list next to the File Inspector. It’s the one with the yellow icon:

Continue reading

How to read keyboard input in C

We can use the scanf() function to get user input in C. Here’s a quick implementation of scanf():

scanf() will wait for the user to press Enter before giving its returned value back to our app. We must define a variable to hold its output. We can even define multiple variables, each of which will be populated with whatever is being typed in until Enter is pressed. Consider this:

Here we wait for three string items to be added (numbers entered will be interpreted as strings). To grab a numeric value from the keyboard, we cam use %d like so (d as in decimal, variable defined as int):

Note the ampersand in front of our variable, without which scanf would populate a pointer. We can also mix and match keyboard input like this:

Demo Project

You’ll find a quick demo project on GitHub, which also serves as a template on how to setup Xcode to open Terminal and allow for keyboard input when the project is run.

How to define a struct in C

Structures (or structs) in C are something like “mini-objects”: they are wrappers around multiple variables, much like the properties of an object. Since plan C doesn’t have objects, these are as close as it gets to them, and they can be quite helpful. Apple uses them extensively (think of a CGRect for example), and it’s easy to build our own.

Here’s how we can do that.

Creating a struct

We can define a struct like this:

Here we create a struct with the name of cube and define three variables inside it. They don’t have to be of the same type, you can mix and match int, float, char and all the rest of the merry gang. The variables of a struct are called members.

When defined, a struct can be seen as a template. To use it, we need to initialise a new instance of the struct and populate the member variables, like so:

Our instance of the struct is called massiveCube, and we can refer to its member variables by dot notation.

The whole setup can be done in one step too, like this:

Note here that we still need to populate the variables. Further structs from the same template can be created as shown above.

Typedef-ing a struct

If we don’t want to create new struct instances with “struct cube myCube”, we can write a type definition for a struct.

I’ve typedef’d previous cube structure as MegaCube. Of course we can choose any name we like here, something descriptive is always a good idea. Again, our friends at Apple to this quite frequently. Now we can create new instances simply by referring to them without the struct word and only use our own definition:

This makes our code more readable and is a way of making C code “our own”.

How to read Command Line Input on macOS

I was building a simple Command Line Tool app for macOS. One thing the app needed was user input, i.e. it should wait for the user to type something that I’d like to make use of in the app.

Turns out it’s a rather complicated affair, and I haven’t found a comprehensive starter guide on how to actually accomplish this.

I wanted to create a Command Line Tool app that was capable of accepting text input from the Terminal window, use it, and then write output back for the user to read.

But that wasn’t enough: I also had to tell Xcode to setup the app appropriately, otherwise the Terminal window wasn’t launched – which is of course necessary for a Common Line Tool.

In this article we’ll do just that: prepare Xcode to launch Terminal, wait for input, and print it out again. Here we go.

Continue reading