Imagine we had an NSString consisting of a pool of characters from which we’d like to pick one at random. Say our pool is ABCDEFGHIJKLMNOP, and we want a single character.
Here’s how we can do that:
// pool of characters
NSString *characters = @"ABCDEFGHIJKLMNOP";
// create a random number
// between 1 and however many characters we have in the pool
uint32_t length = (uint32_t)characters.length;
NSUInteger random = arc4random_uniform(length);
// make a range
NSRange range = NSMakeRange(random, 1);
// pick a random character from the pool
NSString *letter = [characters substringWithRange:range];
The comments speak for themselves. One thing of note is the typecast of characters.length, which is needed to suppress the Xcode warning “implicit conversion loses integer precision”.
Most of the magic is provided by the NSString method substringWithRange.
Xcode 9 has introduced a few changes in regards to Version Control is now handled. If you’re looking for the once so proud Source Control – Configure menu option, you’ll find that it’s no longer there. Dang! Where has it gone, and how are we supposed to manage our projects now?
Turns out there is a new tab in town, next to the Project Navigator (File manager icon) in the left hand pane of Xcode 9, called the Source Control Navigator. Click on it to find a plethora of options:
And would you look at that: for the first time in a decade, we can actually manage Tags as well as Branches here! And we get to see all those commits and comments we’ve been making for years, all without having to use additional version control tools! It’s like Christmas has arrived early!
Here’s a WWDC video on how we’re meant to use the new features from now on. It certainly didn’t answer all the questions I had, so for that, read what I’ve learnt through experimentation further down. Continue reading
Since Xcode 7 we can now use Storyboards for the development of macOS apps. While that’s a welcome addition, not everything works as straightforward with macOS and Storyboards as it once did without them (Cocoa Bindings for example is still a huge mystery to me).
In Xcode 9 we have once again a choice when starting a new macOS Project, a simple tick box we had lost over the course of Xcode 8 and Xcode 7. For those of us who are still looking at Xcode 9 as “a little bit beta” and still like to work with Xcode 8, here’s a quick guide on how to create a new macOS project from scratch using good old fashioned XIB files with Xcode 8.3.3.
Let’s go through this process step by step, as we’ll have to do the whole setup manually. It’ll be very exciting, and a nice exercise, I promise!
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.
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.
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.