Do you need help on a specific subject? Use the contact form (Request a blog entry) on the right hand side.

2016-04-30

Moving a xcdatamodeld from one project to another

I have an old iOS4.2 app that I want to rebuild in Swift for iOS9. The old app used CoreData and I did not want to recreate the datamodel but simply copy it over. The main reason is that I want users of the old app to be able to preserve their data in the new app.

Wel, it turns out "simply copying" it over did not work.
There are some reports on the net that cover this topic for older versions of Xcode still using Obj-c, but none of those worked for me.

Here is what I ended up doing:

1) In Xcode create the new project
2) Exit Xcode (just to be sure)
3) In the finder go to the <name>.xcdatamodeld and right-click "Show Package Contents"
4) You should see a file <name>.xcdatamodel. Open this in a text editor that will not add formatting. (I used TextWrangler)
5) Select all and Copy
6) Go to the new project and do the same: "Show Package Contents" of <name>.xcdatamodeld, open up <name>.xcdatamodel
7) Select all and Paste (i.e. change the content of the new xcdatamodel file such that it is a copy of the old one)
8) Save the new xcdatamodel file
9) Start up Xcode again, open the new project and you should see the old data model.

Happy coding...

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.

2016-04-27

Replacing UIAlertView with UIAlertController

Now that I am taking a short break from OS-X programming to do some iOS development one of the first things that I encounter is the replacement of UIAlertView by UIAlertController.

Not to difficult, and I actually like the new way much better than the old.

This was the old code (still in Obj-C):

    UIAlertView *av = [[[UIAlertView alloc]
        initWithTitle:@"Error"
        message:@"Oopsie daisy"
        delegate:nil
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil] autorelease];

    [av show];

This is the new code (in Swift of course):

    let alert = UIAlertController.init(
        title: "Error",
        message: "Oopsie daisy",
        preferredStyle: UIAlertControllerStyle.Alert)
      
    alert.addAction(UIAlertAction(
        title: "OK",
        style: UIAlertActionStyle.Default,
        handler: nil))

    
    presentViewController(alert, animated: true, completion: nil)

I specifically like the way action items are added to the alert view. Makes a lot of sense, and we can associate a handler (closure) with each action. It is no longer necessary to demultiplex in a single action handler.

Happy coding...

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.

2016-04-24

Swift Code Library: Printing a collection type with separators in between

Sometimes I need to create a string of values from a collection type with a certain separator in between them.

An integer array would look like this: 1, 2, 3, 4
Or this: 1 - 2 - 3 - 4

Writing an operation that does this should be as universally usable as possible. The best way is to add it to the CollectionType protocol.

This was the method I ended up using:

extension CollectionType {
    
    func descriptionWithSeparator(separator: String) -> String {
        return reduce("") { $0 == "" ? "\($1)" : $0 + separator + "\($1)" }
    }
}

It was the shortest code I could come up with, but maybe not the fastest. I have not tested for speed.

Happy coding...

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.

2016-04-19

Xcode Server and multiple repositories

Last week we upgraded our mac mini server such that it can now run the most recent OS.
And of course we installed OS-Server on it. A big step up from the old days of OS-X 10.7...

And of course we wanted to setup continuous integration (CI) for XCode.

But we hit a snag that cost me a good day to figure out: our first bot could not find all the necessary source files. It just so happens that we use Xcode's workspaces and in a workspace we have multiple projects:



In the project Swiftfire we use the files ASCII.swift and VJson.swift from SwifterJSON. And of course these are referenced files, i.e. Swiftfire does not have copies of those files, but refers to SwifterJSON whenever it needs the files for compilation.

That is a problem when creating bots for CI.

One of the first things a bot does is to check out the repository of its associated project. And since I created the first bot inside a project instead of the workspace it failed to check out all of the necessary files.

Inside the workspace however it is not possible to create a bot since the bot must reside in a repository for the server to check it out.

I have not found a way to create a git repository from inside Xcode when creating or working in a workspace. So I did this manually.

Note: Before doing so, it is worth noticing that git works recursive. Hence it is probably not a good idea to have a workspace repository sitting in a directory in which the projects also reside. Unless you want to tweak the '.gitignore' file. I have not tried this, but opted for the safer approach: I created a special "workspace" directory alongside the project directories:


In the workspace directory, using Xcode, I created the Q5 workspace.
Then I exited Xcode (seems safer to me) and started the Terminal. I navigated to the Workspace directory and initialised a git repository: > git init
For good measure I also added a ".gitignore" file with ".DS_Store" and "xcuserdata" in it:



At this point we may well also add and commit the files:
> git add .
> git commit -m "Initial commit"

Next I fired Xcode up again, and look, it found the workspace repository:



Using "Source Control" -> "Workspace - Master" -> "Configure Workspace" I created a new repository on the server. If you did not add & commit at the Terminal line, you need to "commit & push" the workspace file now.

One more thing to do: we now must tell Xcode which repositories are necessary when a bot is activated. This is done in the "Source Control" -> "...." -> "Configure ...." dialogue:



For each project that is needed, make sure the "include as" is checked and the popup box has selected "required".

I forgot if it is necessary at this step to perform a commit & push again. It probably is.

Anyway, you can now create bots and when executed each bot will checkout all projects and find all files necessary for its integration.


Happy coding...

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.