Category Archives: Fixes

iOS project gets: “_objc_read_weak”, referenced from:

I got this recently from XCode4 – not the most obvious of error messages, and google had zero results. So, to help anyone else who gets the same problem…

What’s happened?

Somehow, you’ve included a library in your project that was written for Mac OS X – not iPhone/iPad.

The problem seems to be (note: I’m not 100% sure of this) the library is using OS X’s Garbage Collection directives. Often, the line above will be just in a few places in the project.

If so, I’m surprised this breaks Xcode builds – I’d have thought they’d have set iOS compilation to ignore this stuff.

Workaround

iOS doesn’t (yet) support GC, and it looks like Apple intends that it never will do. If you’ve got access to the library source code, it seems you can usually just remove the “weak” directive, it’s there to provide a hint to GC, and isn’t actually needed.

Obviously, you don’t want to break the OS X code, so the typical workaround is to take the original line of source:

@property (nonatomic, readonly)  __weak  NSObject *thing;

and wrap it in an OS X only conditional compile:

#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
@property (nonatomic, readonly) NSObject *thing;
#else
@property (nonatomic, readonly)  __weak  NSObject *thing;
#endif

If you don’t have the source to the affected library…

…then you’re stumped. Best I can suggest is to implement the _objc_read_weak function itself, and make it effectively do nothing – but I haven’t looked at the Apple docs to work out when/why/how it’s invoked.

Loading SVG files on iPhone/iPad: SVGKit (not the javascript one!)

UPDATE SPRING 2012: The main SVG lib described below (SVGKit) now works a LOT better, and is only a short way from loading all mainstream SVG files.

NB: it also has a new URL (due to GitHub’s internals) – https://github.com/SVGKit/SVGKit/

I’ve got a bunch of SVG files – hand-drawn maps for a computer game – which I wanted to load onto iPad, and port the game. The files are standard, straight out of Inkscape (the most popular free vector-editing / SVG-editing program).

Unfortunately, Apple doesn’t provide any rendering-support for SVG, and the nearest they do provide – PDF rendering – is slow and uses too much memory. PDF isn’t easy to render, and it does a great job on accuracy, but even on OS X, Apple’s API is often too slow for real-world use.

So, a quick look around the open-source alternatives…

SVGQuartzRenderer

This is the library I found via googling. It sort-of works, and it was only designed for a very narrow use-case (so: fair enough) … but it has some pretty major problems, and it’s not really workable for general SVG loading:

  1. The project is badly organized – it has iOS-only files referenced in the OS X project, and vice versa
  2. You can’t do a simple “build this and use in another project” – there’s source bugs/typos that have to be fixed by hand first, mostly because of the bad project organization.
  3. The interface to your Cocoa / iOS app is weak – it’s not designed to integrate cleanly, and it seems to jump you through hoops that most programmers would find makes life hard

SVGKit

While trying to fix SVGQuartzRenderer, I found SVGKit by accident. There are two projects with this name – one is a Javascript library, the other is an Objective-C library. They appear to have nothing in common except the name.

UPDATE SPRING 2012: new URLhttps://github.com/SVGKit/SVGKit/

SVGKit worked pretty well out of the box – you can do a build direct for iPad from their sample project, and it loads up into an “SVG Browser” that lets you try a couple of example SVG’s included in the project.

TL;DR – do NOT download the main version, it’s out of date and buggy, and the original author has disappeared. But there are high-quality forks you can download instead that work very well (instructions below).

UPDATE SPRING 2012: the project now has a team of contributors and admins, and the “main version” should soon be up to production quality

SVGKit installation / getting started

Tragically, the install instructions on the front page of the github project are simply … wrong. There’s no way you can use it in your own projects with those instructions. The sample projects work, so it was a case of doing a line by line compare to work out what special magic settings were needed.

UPDATE SPRING 2012: All the instructions have been re-written (I updated a lot of them myself :)) – use the instructions on the https://github.com/SVGKit/SVGKit page

Corrected instructions are:

  1. Drag/drop (or copy/paste) the “iOS” and “Core” folders into your Xcode project
  2. edit the Build Phases, and add to the Libraries phase: libxml2 (this gets Xcode4 to add it as a framework/dylib)
  3. edit “Header Search Paths” and add /usr/share/include/libxml2 (this gets Xcode4 to actually USE it when building)
  4. edit your Build Settings, and set “GCC_VERSION” to be: “com.apple.compilers.llvm.clang.1_0”
  5. add “QuartzCore.framework” to your iOS project.

The only one of those that’s particularly unusual is changing the Compiler to LLVM 1. If you don’t … the project won’t compile.

SVGKit: basic SVG files … use Reklis’ fork

Then I started opening Inkscape-generated files in SVGKit. Sadly, most files in Inkscape will – literally – crash SVGKit. There are major bugs in the parser, a combination of mis-reading the SVG spec, and a bit of really sloppy C code (buffer overruns in the parser! Ouch).

Fortunately, the parser has been completely rewritten by reklis, who’s fork seems to be now the “de facto” release of SVGKit. (incidentally, I tried emailing the original author, who I think is the only one that can update the main project, but his website email link is broken :(. I’ve tweeted him too, hopefully he can pass-on the project to reklis to maintain).

UPDATE SPRING 2012: … the original author has now made it a shared project, with multiple admins.

…but there were still some major bugs. More than half of the Inkscape files wouldn’t render even vaguely correctly (although all the crashing bugs are fixed).

I delved into this, and it turned out to be two very small remaining bugs in the parser, which I was able to fix quite quickly, and merge back into reklis’ fork – if you grab his fork now, it should work pretty well.

SVGKit: …advanced SVG files

Unfortunately, the really complex / rich SVG files still fail in SVGKit. With my fixes, most of them render approximately correct – e.g. I have a version of the famous Tiger.SVG which gets the main outline exactly correct, but loses all the internal colours and objects.

Others – such as the almost-as-famous Lion.SVG – fail completely, you just get a big blob of colourless trash on screen :(.

UPDATE: Lion.SVG now works OK. Colours are slightly wrong, but the complex image is rendering well – you actually get a lion now 🙂

However, all the Inkscape-authored files I’ve got are working fine, with all the tools (lines, curves, “freehand”) working as expected. That’s enough for most developer projects.

Guesses at fixing the remaining SVGKit bugs…

Just in case anyone reading this needs a perfect SVG renderer on iOS, and has the time to try and fix it, here’s some thoughts on what to look at next.

My guess is that the vast majority will render the correct *shape* (I’m confident those bugs are all fixed) – but there are some advanced curve shapes that no-one has implemented yet. I suspect that Lion.SVG is using them, hence why it’s such a big mess when it renders.

Separately, I think there’s an issue with z-order layering and/or the implementation of the “fill” command in SVG, that’s causing things like Tiger.SVG to be the right shape, but missing all their internal detail.

Hopefully, someone else can fix those soon. For now, I can say that everything I’ve got / made in Inkscape is working fine, which is good enough for me to get on with my game project.

UPDATE SPRING 2012: There is experimental support for CSS styles (which make the colours correct for every SVG file) – it’s not yet merged to the main fork (still needs testing).

Also, there is partial support for the “transform” attribute – currently only handles translation, not rotation. So, for instance, SVG files that have rotational symmetry (e.g. compass roses) fail badly. But this is coming soon…

Finally, there is partial support for SVG Text.

When all three of those have been tested and merged, SVGKit should be loading almost all SVG files correctly.

Simple Android template for new Game using an Entity System

If you’re working with Android, you quickly find that Google forgot to include some core things in the OS. Getting a “Hello World” application to run on your phone requires many hundreds of lines of code, 90% of which you’ll never change from application to application (i.e. it should have been built-in to the OS).

NB: you can do a fake “Hello World” on Android in literally 10 lines of code – but it’s not a real app. It only exists to pretend that Android is correctly configured by default – i.e. it’s a marketing hack :).

If you’ve been using the free Entity System libraries over at http://entity-systems.wikidot.com, the Java versions need to be manually integrated into each new Android project. The Objective-C versions don’t need any integration – the default templates for iPhone/iPad projects work fine – it’s just the Java/Android ones that need work.

So, here’s a pre-made template you can use for starting new Android games / Entity-system projects. It starts up and draws a starfield, so you can confirm that the render-loop is running, and animation is working. It also shows that auto-rotate is configured and running (by default, Google doesn’t provide this):

https://github.com/adamgit/Android-Starting-Project-with-basic-Entity-System

NB: the code provided works fine – but the Activity-integration could probably be done a lot better. I just ripped it out of an old project, so I’m sure it works – but it could be a lot more elegant.

Post-Install

The Entity Systems libs are constantly being updated, so the project above does NOT include any particular version – you have to download the lib you want separately. Install instructions are in the README (which also shows up as the main body of that webpage above).

NB: if you don’t know what Entity Systems are, none of this is of help to you! Go have a look at Entity Systems are the future of MMOG development – Part 1.

(it’s a coding / design technique for making computer games faster / easier to write and maintain)

“CodeSign error: Certificate identity … appears more than once in the keychain. The codesign tool requires there only be one.”

One morning last week, I fired up Xcode4, and all projects were refusing to build. The Apple Developer certificate had expired (why does Apple make this expire *earlier* than your annual iOS $99 subscription? Odd).

Anyway, a quick trip the Apple Developer Portal, and the Provisioning page, one-click “renew” certificate (free), 30 seconds refreshing for Apple to process it, re-download, delete the old one (using the Apple app: Keychain Access), install the new one, re-build.

FAIL

…only to be hit by this error message:

“CodeSign error: Certificate identity ‘iPhone Developer: XXX (YYY)’ appears more than once in the keychain. The codesign tool requires there only be one.”

What?

I just deleted that, in Keychain Access. I selected the one that was expired (it’s highlighted with a red error icon), and hit delete. It then disappeared. And allowed me to install a new one.

Xcode4: Clean, re-build. Nope, still refusing
Xcode4: restart, delete all build directories. Nope, still refusing.

Symptom: Apple’s “delete” does not “delete”

It’s a bug – a HUGE bug – in Keychain Access (Apple’s app that is part of OS X).

When you hit “Delete” (either the delete key, or right-click and select the Delete menu item) … Apple 50% of the time doesn’t process the deletion.

However, 100% of the time, Apple makes the item disappear from the screen.

To check if it’s deleted, close Keychain Access and re-open. If the delete was ignored, then you’ll see that the certificate you deleted has RE-APPEARED:


(third-from-the-bottom, you see the cert I had already deleted … has come back from the dead)

Solution: Delete and delete and delete until Apple stops ignoring you?

I’ve found that if you switch to the “All Items” view, on the left hand side, the deletion seems to work every time. But maybe that was just blind luck. Even if it fails, the act of switching back and forth between “All Items” and “My Certificates” seems to force KA to refresh the view, and tells you if the delete has “worked” yet: