Twitter

I registered for Twitter ages ago, but only recently started actually using it all of the time.

http://twitter.com/schinckel

Xcode 3.1

I finally got around to updating Xcode to 3.1 yesterday.

There’s some great stuff in there. At last, the autocomplete of method names puts in tokens for the arguments, so you can just click on the token and then overtype – or just delete it in one go with (forward) delete.

Another cool thing is something that I’ve seen in NetBeans, but the default settings there for it are just annoying. Previous instances of the currently selected variable are highlighted. In Xcode it is a much subtler underline, much better than bright yellow.

This is better than NetBeans in another way – you can click on the item, and access a special contextual menu that allows you to edit every instance of the variable name in your current scope. Thus, refactoring is made much easier.

I’m sure there was something else I really liked too, but I can’t recall what it was.

NSScrollView copiesOnScoll Slowdown

I have a custom class, called MultiMatrix, which is basically an NSMatrix that has header rows and columns. Selecting a header row or column will select the whole row/column, and selecting the top-left cell will select the whole table. These header cells will also automatically select themselves if their whole row/column/table is selected.

Because I dynamically update the size of the matrix according to some data elsewhere in my program, I needed to embed it into an NSScrollView, so that when the size exceeds the normal size, then users can scroll to select cells.

I did this, and I was having huge slowdowns. I could select cells alright, but selecting a header cell meant about a one second delay.

Solution: turn off the NSScrollView’s copiesOnScroll.

Even though I wasn’t scrolling, for some reason this was causing big performance issues.

I suspect it’s making my custom matrix class do something lots of times, or perhaps it’s related to the controller and the bindings I have set up. Regardless, unchecking the Copies On Scroll box in IB fixed it.

Boolean and Integer values in C

    Tags:
  • c

I got tripped up by how C represents Boolean values today.

To save space, but more to make comparisons easy, I was storing a series of bits (representing hours in a day) as integers. I had built up some structure so that my sub-classed NSMatrix could use Cocoa Bindings and be connected to a Controller, so that changing the values in an NSArray of Integers in that controller would change the selected cells in the matrix, and changing the cell selection similarly affects the values stored in the array.

But, and it took me a while to figure this out, I was having problems getting the data back from the array properly. For some reason, numbers greater than 255 were not displaying properly. Basically, I seemed to be losing bits off the top. So I spent ages rewriting the code using longs, long longs, and unsigned long longs, but nothing seemed to work.

Eventually, I pinpointed the error to this: in C an integer with the lower 8 bits all set to 0 evaluates to FALSE.

It doesn’t seem to matter if the integer is a long, or whatever.

Thus, when I checked, using a bitwise AND (& operator), the stored value against a value representing the cell value, and stored it directly in a BOOL, and then used said BOOL to set the selection of that cell, it was evaluating to False for all values greater than 255.

All I needed was to change one line of code:

BOOL highlight = (value & bin);

becomes:

BOOL highlight = (value & bin) > 0;

All fixed.

XCode 3.1 and "Availability"

You can’t have a class with the name “Availability” in a project built with XCode 3.1.

You can with XCode 3.0, but CFNetwork, and I assume a heap of other classes use a class called “Availability” to define if objects are available in a particular version of the OS.

Public Transit ain't that great

A couple of weeks ago, there was a puff piece in the local newspaper about a guy and his wife (and teenaged daughter) don’t drive, don’t even have a car. They catch public transport everywhere, and it works for them.

Adelaide is a funny place. It’s not really densely enough populated to have a first-class transit system, and because it isn’t really that good, not many people use it. This is possibly also in part due to how easy it is to drive everywhere in Adelaide, including into the CBD.

I’m a big fan of public transport, it just isn’t working that well here right now.

I used to work in the CBD, and if you live/work/play/study in the CBD, then Adelaide’s public transport is fine. I used to be able to stagger up the hill to the nearest (well, second nearest, as they closed the nearest) railway station, and as long as I was there by 8:02 am, I would be in time for work. Unless a freight train happened to come along at 8:01am, and I wasn’t able to cross the tracks until too late to catch my train!

I had another option, which was to wander a little bit down the hill, and catch a bus. But buses are much less reliable (yes, even than our pathetic train system here in little old Adelaide). Since I am near the terminus, then theoretically, the buses shouldn’t be late, unless the previous bus was so late that it made this run late. Which happened about 50% of the few times I caught the bus into town. The train was more convenient anyway, since it (usually) stopped closer to my work.

This year is a different matter. I am studying at the University near my house. It takes about two “three-minute pop songs” to get there by car. Seven minutes. And that’s from my driveway, to the carpark that is a one-minute walk from my building. Meaning I can leave home at 8:50, and even if the traffic is slow, still make it to a 9:00am lecture on time. If I’m going to a 12 noon lecture, I need to leave earlier, since there is never enough parking, but that is another matter.

Actually, I guess it’s related to what I’m about to rant about.

If I catch the bus to said 9:00am lecture, guess what time I need to be at the bus stop? Remembering that the transit time for a car was 7 minutes.

I need to be at the bus stop a full hour before my class starts.

One hour.

If the bus is on time, which is unlikely, then I’ll be early for my class. If the bus that is scheduled for half an hour later was on time, then I’ll arrive just in time for class. However, the buses are never on time. I don’t know if they are early or late, as it’s hard to say whether the bus that actually arrives is 3 minutes early, or 27 minutes late. I have had instances of me having been at the bus stop well before the due time of the bus, and having had to wait 45 minutes.

Now, this bus is traveling against the “peak” flow of traffic, and it is still managing to be off schedule.

I used to live on a bus route that was better serviced, but then I was traveling into town, but more importantly, it was a “smart” bus stop that told me how long it was before the next bus was coming. This would be useful for me now, since I can walk 10 minutes down the road to another bus route (but, let it be said, that doesn’t seem to make any difference to my travel and wait times, as the buses all come at the same time!) However, since I am going “against the flow” in terms of my direction, it is likely they wouldn’t even bother with e-boards on my side of the road.

I think one of the problems is that the transit authority in Adelaide don’t like to change timetables. They see this as a big issue, and will only change timetables about once or twice every three years.

Traffic conditions change much more regularly than this. I’d like them to reexamine every couple of months, and tweak the service. The worst thing about the system is that it doesn’t meet it’s timetables. Why not change the timetables? Make it a realistic setup, instead of forcing the drivers to stick to timetables that obviously aren’t maintainable.

I’d really like to see a decent electronic system. A combination of the smart stops, but better electronically available timetables. Not just PDF copies of the timetables on the website, but a proper, searchable system.

I envisage a Google maps-like page, where you can choose your start and finish points, and the system shows you all of the possible transit options between these points (including connections, if necessary), and when the vehicles are scheduled to run.

For instance, I could say I wanted to get into Adelaide from my house, and it would show me the next departure time of the train, and the bus, and the relevant arrival times.

I can currently use the “Nomad on Board” crappy system on my phone, but I need to know in advance the route numbers, and it’s really not efficient to compare them. And the times are out – it seems to think I’m in a different timezone to what I actually am anyway.

Missing NSButton Type

I’m trying to make a control that allows me to indicate when a record in my application is synced with the AddressBook person of the same name – I have made it so that I can only enable syncing when a person with the exact same name exists in both places, but to indicate it, I want to use a button that shows a pressed-in state, but also alters the image.

I want the image to be black-and-white by default, but when pressed in, it becomes coloured.

Apparently, this isn’t possible. You can either have a button which changes it’s image, or displays a pressed in look.

Might have to subclass it.

Update: You can subclass it, but there’s no need to. Just give yourself a reference to it (myButton, for instance), and then:

1 [[myButton cell] setShowsStateBy:NSPushInCell | NSCellLightsByContents | NSCellLightsByBackground];

Escalator Spinning

Gotta love this.
via Mister. D

Face Transformer

This is pretty cool. Upload an image of a face, and you can run some transforms on it to get alterations.

Try it out at http://www.faceofthefuture.org.uk/

For instance, from two pictures of me, here are some of what I may look like as an “Older” adult:

Picture 1.png Picture 3.png

The one on the top actually looks a little bit like my grandfather, and the one on the bottom looks an awful lot like my father. But older.

Some other funny ones. Afro-Caribbean:

Picture 4.png

East and west asian:

Picture 5.png Picture 6.png

And, saving the best until last, two different ape-men:

Picture 7.png Picture 2.png

NSSegmentedControl and Core Data.

It’s not possible to directly hook up an NSSegmentedControl (or NSSegmentedCell) to a Core Data controlled NSArrayController (or any, for that instance). I wanted to be able to use one of these controllers to allow the user to select one, many or all items from the array.

To define the behaviour a little bit more: You can select the first item in the control (which will be called “All”), and it will select or deselect all of the other items, depending on what it’s state becomes. If you click on another segment, then it will toggle the selection of that segment. If all segments are ON after pressing a segment, then the first segment must also display ON, otherwise it will be OFF.

I did it by creating a new class: SegmentController. (I know, I should put on a prefix…)

SegmentController has two required IBOutlet variables, which refer to the NSSegmentedControl, the NSArrayController, and a property NSMutableIndexSet, which stores, sets and gets the selection indexes (I think this should be indices, but I’ll stick with Apple’s convention). I also have a sortDescriptors array, so that this object can sort the array controller.

There are a couple of limitations, which I might look at how I deal with – for instance, the NSSegmentedControl must have exactly one more segment than the NSArrayController has items. I’m using it to select one or more days, and this number doesn’t change. It should be possible to dynamically create the right number of segments, and populate them with values (I even have a readonly field called shortName set aside in my Day objects for this), but at awakeFromNib the NSArrayController is still empty, and I can’t figure out a nice neat way to force a fetch.

Hooking up the elements in Interface builder is easy. Create the NSArrayController, and set that up however you need to. Do the same with the NSSegmentedControl – at this stage you’ll need to put values in each of the segments. Now, create a new instance of SegmentController (you might need to add the class files to your XCode project first). Connect the two outlets up to the required objects.

Now, in the Bindings Inspector, connect up you’ll want to make the Selection Indexes parameter point to the selectionIndexes model key of your Segment Controller object.

Finally, make the selector of the NSSegmentedControl point to the selectSegment: action of the Segment Controller.

I did notice a slight delay between deselecting one segment and the “All” segment deselecting. A simple optimisation – moving the call to segmentCount out of the loop test and into a local variable made it much smoother.

That should probably do it. It might need a bit of tweaking. You should be able to have multiple instances of this in your project, if it is required. It’s been designed that you’ll need a seperate one for each Segmented Control, as it refers to the objects it is connected to, rather than the sender of the message.

My source code is here: SegmentController.zip