What’s a UITextEffectsWindow? And why is it receiving messages? 17 September 2009
Posted by ojmason in Apple, iphone, objective-c, programming.1 comment so far
I just spent several hours (or at least it felt like several hours!) in frustration, searching a trivial bug. I’ve been testing a quick’n'easy prototype screen with an UIImageView and four UIButtons. The buttons are linked via an action to a view controller. And every time I press a button, my app conks out complaining that -[UITextEffectsWindow buttonPressed:] was an unrecognised selector. I checked the memory address, and it said it was my view controller, just before that exception was thrown.
I was ready to put the blame on some mistakes with Interface Builder, until I came across the solution (indirectly) in a blog: here the problem described related to properties, and the difference between “vc = …” and “self.vc = “. I had another look at my code and quickly found the offending line: I had the view controller as a local variable in the app delegates viewDidLoad method, and I autoreleased it. In other words, by the time the button was pressed the view controller no longer existed, and hence I got that weird error message.
This was not helped by the fact that UITextEffectsWindow is not mentioned in the documentation anywhere, as it seems to be an internal UIKit class, but at least it appears to be consistent.
So, if your button presses send messages to UITextEffectsWindow, make sure to check that your view controller is still alive!
Application Promiscuity 7 September 2009
Posted by ojmason in Apple, iphone, objective-c.1 comment so far
I was getting a bit bored with the slow progress on my Esperanto dictionary app, and over the holidays I started work on a few other ideas I had. One was a Maths-drill program for kids, as the ones that are already out there (at least the ones I tried) don’t seem to be ideal (nothing ever is ideal, though!). So I tried writing another app so our kids could play and practice their maths skills.
That app is almost done, just the artwork and sound effects are missing. At the moment it looks pretty rubbish (but looks aren’t that important as long as it works and doesn’t crash!), and the sound effects are nicked from somewhere, so I have to replace them with free ones. Again, the purpose was to try things out.
That app was quite fun, and also easy to do. More on that later…
The next app is one that supports teaching and learning students’ names. This makes use of a navigation controller, which is slow going. I’m picking up loads of experience in Objective-C quirks along the way. For example: avoid using NSNumbers as the keys in a dictionary if you want to save it using writeToFile later on…
Overall it’s very exciting, and the iPhone is a fun platform. It’s really great to see your own stuff amongst all those polished apps, and provides great motivation to do better.
NSData Naughtiness 13 July 2009
Posted by ojmason in Apple, objective-c, programming.add a comment
Well, this is not exactly NSData’s fault, but I ran into a problem (for the second time; the first time I bypassed it with a short-cut) when reading text data from a file.
Occasionally there was random garbage at the end of a line, which I could not understand. Incidentally, I was reading a number of full files in one go each into an NSData instance, and converted that into an NSString with the correct encoding; this I would then tokenise and add to another file. So the garbage was actually at the end of each file. I then found that I can directly initialise an NSString with the contents of a file, and the problem disappeared.
Now I want to produce concordance lines, and I jump into the middle of the file to read a stretch. First I run into trouble with the encoding: as the data is UTF8-encoded, a random jump can end up in the middle of a multi-byte character. NSString does not like that… but here I can just test for that and skip the initial bytes. The same problem obviously also happens at the end, where the final multi-byte character could be incomplete. Again, truncation seems the easy way out.
But I also then had the issue with the occasional random garbage again! NSData seems to be at fault, and this time I can’t bypass it, as NSString can only read a full file. Quick websearch, and the solution crops up (in an aside) on stackoverflow.com: the data that NSData returns from the -bytes method is not zero-terminated, but NSString’s -stringWithUTF8String expects that, hence the random garbage of the unterminated data. In a way I’m surprised that it actually worked most of the time!
Dictionary Dangers 9 July 2009
Posted by ojmason in Apple, objective-c, programming.add a comment
I just spotted a bug that cost me several hours yesterday, without having a clue what was going on. I’m currently working on a program which indexes texts, and as I encounter words, I add them to a NSMutableDictionary with their positions in the text. All was working well, until I tested it on a bunch of texts, and it failed with some obscure message about key-value coding. I then added some NSLog messages, and discovered that the token it failed on was the at sign, @.
Today I had another look at the documentation of NSMutableDictionary, and especially the method valueForKey: where the problem seemed to occur. And there it was: “If key does not start with “@”, invokes objectForKey:. If key does start with “@”, strips the “@” and invokes [super valueForKey:] with the rest of the key.” Suddenly it dawned on me: I was using the wrong method. Instead of valueForKey: I should have used objectForKey: – the plain @-sign is discarded, and leaves an empty key (which did of course make the error message less comprehensible, as I couldn’t really tell it was empty).
Quick change in the code, and it works. Problem solved!
Lesson learned: always pay close attention to the available methods, and make sure it is the one you want, even if the one you’re using sounds like it’s the right one. And read the docs carefully!
Making Progress! 26 June 2009
Posted by ojmason in Apple, iphone, objective-c, programming.1 comment so far
DAY 11 and the pace is picking up. I managed to sort out two of the four tabs of the app, one including a table display of Esperanto/English dictionary entries (with the proper Esperanto diacritics), and the other one being a live-search on the English gloss entries. This one even pops up an automatic keyboard when first selected; the tab bar controller sends it a message when it has been activated. I even managed to answer a question on stackoverflow.com on that issue.
I also found somewhere a hint on how to automatically shrink the label size if the text is too long, wrote a converter from the Esperanto x-notation to unicode, etc. Really pleased! Both of these will appear here later.
One thing I don’t like too much is writing all the UITableViewDelegate methods when using a table. Quite a lot of boiler-plate code, but then, it is quite powerful. I just couldn’t be bothered writing yet another set of those methods for the final tab, the info view part. But there is nothing technically difficult with it. Objective-C is also getting easier and easier, and the auto-completion feature and API doc integration of XCode really helps. Though I think for serious code I will still use vile…
That leaves pretty much only the core part of the app: the Esperanto morphological analyser. I will implement this as a finite state machine, and working with Cocoa has given me the idea to implement it using delegates; an abstract hull which calls methods on the delegates whenever it needs to retrieve data, or match something. I have the feeling that this is pretty similar to Erlang’s OTP frameworks.
It is a really rewarding experience to see your own app on the iPhone. If only I was better at designing the tab bar icons! And I can already foresee one objection: one of the tabs has a star icon, the star being the Esperanto symbol. I think I need to change that, as the star is reserved for the ‘favourites’ meaning, and I don’t think Apple would appreciate the use of a star (even if it looks slightly slimmer) with another meaning.
Every character is important 23 June 2009
Posted by ojmason in Apple, iphone, objective-c, programming.1 comment so far
DAY 10, and a little bit of progress. I found one error, which was about to drive me mad, until I more or less by accident stumbled over the solution on stackoverflow.com; in answering a slightly different question, Rob Napier commented on a bug in the way NSLog() was used.
NSLog(@"Returning %@ rows", [nodes count]);
Here, the -count method returns an int, but the %@ symbol expects an object. Result: a bus error. You need to use %d instead. And here was I, thinking how great it is not to bother with the old printf format codes, using the %@ way instead. I hate the primitive type/object distinction!
I then also found that I had created two superimposed Table Views (did I mention that I don’t really like Interface Builder after all?), so that my table got overwritten and looked all wrong. Now I’m struggling with getting the table to start at the first entry, not the 35th (indexPath seems to start counting sections at 1, rather than 0), and all of a sudden my database won’t work.
Preparing a straightforward SQL statement suddenly fails. I did not change that part of the code, the database file is still the same as it used to be before the error occurred, and I did tell XCode to do a full clean. Still, the error remains.
Grmph.
Tab Bar Errors 22 June 2009
Posted by ojmason in Apple, iphone, objective-c, programming.add a comment
DAY 9 and more frustration. My ViewControllers didn’t seem to be called, and NSLog messages I put in didn’t appear on the console. And the whole thing crashed without me knowing what was going on.
Having another look at my iPhone development book, I realised the first mistake: the TabBar Controller wasn’t aware of which controllers to call up for the various tabs, something you need to set up in Interface Builder. Still don’t really like that way of doing things, as the fact you’ve done it (or not, as the case may be!) is not easily transparent.
After completing the tab bar attributes, the log messages suddenly turned up, which was good, and it also doesn’t crash. Well, at least not where it used to crash. Now it crashes in the loop where I read stuff out of the SQLITE database, and it crashes after the second iteration. Wonder what the cause is. Probably memory management. Mixing Objective-C and plain C makes things a bit confusing. And I don’t like that NSString is an object you need to allocate, but NSInteger is basically just an alias for int, so a primitive data type rather than an object.
Anyway, more bug hunting coming up. It all feels terribly slow, but then I’m only spending a few hours every couple of weeks on it at the moment.
Table Frustration 31 May 2009
Posted by ojmason in Apple, iphone, objective-c, programming.add a comment
DAY 8 and after a long pause I have been able to spend a bit more time on my project. And it was certainly frustrating, racking up the hours while trying to work out why it didn’t work. One thing I need to get used to are IDEs; more than a decade of simply editing code in vi and compiling it on the command-line leave you ill-prepared for working with XCode, nice though it may be. Interface Builder is also a great tool, but it hides things from direct view or easy inspection, at least if you don’t know where to look. And then something doesn’t work because you omitted a link from a View to its File Owner, or you set the base class of MainWindow.xib to a wrong class type.
After trying out some table code in the iPhone Development book I finally got a small demo going. I’m now trying to create an indexed grouped table display backed by a sqlite database working. But now it’s approaching midnight and I’m too tired to work out why it stopped working again…
However, despite all of today’s frustration, I’m getting more and more used to coding in Objective-C, which has to be a positive side-effect!
Further Education 11 May 2009
Posted by ojmason in iphone, objective-c, programming.1 comment so far
I’m too busy with marking and a paper I have to write to get stuck in again. On the way home today I had a thought about the database-backed table view it will include: something regarding sorting which I came across when working at the Cobuild project. More on that later.
But I’m still using the time more or less productively by watching the lectures on iPhone programming from Stanford (available on iTunesU). Not too sure about the performance aspect, but it is great to be able to listen to those guys explaining the intricacies of Objective-C memory management. And I won’t complain, as it is freely available. Thanks for that, Stanford!
Building the database without glare 5 May 2009
Posted by ojmason in Apple, erlang, iphone, objective-c, programming.1 comment so far
DAY 7 sounds like a lot, but again I’m only working a few hours in the evening after the kids have gone to bed. I have the feeling that there is a lot of ‘boilerplate’ code to write in Objective-C, or Cocoa at least. But then, that might be the problem with GUI-related programming. Erlang doesn’t nearly need as many lines to accomplish something, anything really! But then, the kind of Erlang programs I’ve been working on are basic R&D text-only affairs, not MVC-style user interface programming.
I have today learned how to turn off the iPhone program icon glare (add UIPrerenderIcon=true to info.plist in the bundle), which I think looks better on my icon which had a horizontal line just about where the glare-line was. I also created a SQLite database from a text file. Next I will need to integrate the DB with the table view, for the first part of actual functionality. I keep switching between chapters in the textbook, the one which builds tables and the one which deals with persistent storage. Why did nobody think of doing a database-backed table view? Need to check Apple’s sample code, they’ve got a book-storage one which might do that.
The depressing bit is that I write a little Noddy-program, and it takes me ages. Mainly getting used to Cocoa, Xcode, and Objective-C, but also going back to no garbage collection, and shifting data between classes I know not well at all (NSString, NSArray, etc). I think my niche will be little utilities, rather than glorious games!