Category Archives: macOS Development

What to do with “Linker Command failed with exit code 1” error in Xcode 8

One of those super ridiculous situations in which Xcode couldn’t be more unhelpful is if it tells you flat out: “Yeah erm… there’s an error here, but I’m not going to tell you where to start looking”. Other than the above error message, we see no log output or any further clue that may help us to investigate further.

Nice going, Xcode! You have such a dry sense of humour….

Lucky for us, there is a way to dig at least a little deeper by manually revealing what’s bugging Xcode: right-click the error message, and select reveal in log.

Now we can read the log file at our own leisure and see if we can make head or tail of it.





How to delete a git branch with Xcode 8

Recently  I tried to merge some changes I had made on another branch back into my master branch, but Xcode wouldn’t let me. Spurious error messages prevented this from happening. I was happy to simply create a new master branch and overwrite it completely with the changes I had implemented on my former testing branch.

Turns out that Xcode is happy to create new git branches for our projects and screw them up several times in a row, but sadly, it is not capable of deleting branches.

So the simple answer to the title of this post is: it can’t be done!

However, a quick command in Terminal can do it for is. cd into your local project directory and issue the following:

git branch -D yourbranch

where “yourbranch” is of course the name of your branch. Make sure you’re not currently on the branch you try to delete.

Doing this allowed me to simply create a new branch using Source Control – New Branch. When we do that, Xcode will automatically use the contents of our current branch as a starting point for the new branch and switch us onto it immediately.





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

git add .

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 for loop
for (;;) {
    // repeat this forever
}

Infinite while loop

// infinite while loop
while (TRUE) {
    // repeat forever
}

Infinite do-while loop

// infinite do-while loop
do {
    // repeat forever
} while (TRUE);

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():

#include <stdio.h>

char input[100];
char *output = "Thank you for your message!";

int main(int argc, const char * argv[]) {
    
    // ask user for some input
    puts("Tell me something nice:");
    
    // grab input from keyboard via scanf()
    // don't use gets() for this, apparently it's totally evil
    scanf("%s", input);
    
    // say goodbye
    printf("%s\n\n", output);
    
    return 0;
}

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:

 char input1[100], input2[100], input3[100];
 scanf("%s, %s, %s", input1, input2, input3);

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):

 int number;
 scanf("%d", &number);

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:

 char string[100];
 int number;
 scanf("%s, %d", string, &number);

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.