Adventures in Objective-C – Part 1

I’ve been trying to get my head around Objective-C and Mac/iPhone programming for the last month or so. At this point, I think I’ve got most of the major concepts down.

As an Object Oriented (OO) language, it bears a certain familial resemblance to Java, which I typically program in these days.

While I’m not going to say that Java is a “better” language, I do feel that parts of Objective-C are a capricious conglomeration of logic-unfriendly syntax.

It’s the implementation of these things that’s irritating. Obviously there’s a certain amount of familiarity and personal preference, but over this series of articles, I’ll highlight a few. I’ll start with a simple syntax example.

Generally in OO programming, you break things into programatic “objects”, which define the properties of an object and the actions that such an object could do.

In Java speak, these are properties and methods, in Objective-C they are properties and selectors. In Java you “call” a method, in Objective-C you “message” a selector. Same thing, different terminology. Clear as mud? Good, then let’s continue.

Typically, properties on an object are protected behind accessor method, such as “getXXXX” and “setXXXX” where XXXX is the property name. Sometimes these are called “getters and setters” – that’s in Java, of course, Objective-C calls them “accessors” and “mutators”. Objective-C also uses a slightly different convention. Mutators are still “setXXXX” but accessors are just “XXXX”, which can result in a little ambiguity as to whether you’re looking at the actual property variable or the accessor selector.

Java uses “dot-syntax” to refer to an object’s methods, so far example, if you have an object called “newton” and it has a method called “dropApple()”, you would access it like this:

newton.dropApple();

Objective-C uses square bracket syntax, so the same thing would be:

[newton dropApple];

…and if each one took a single parameter, they would look like this:

newton.dropApple(velocity);

vs

[newton dropApple:velocity];

It gets a little muddier when we move to two parameters.

In the example above, I’ve passed a variable called “velocity” which we’ll say is of object type “Speed”. Let’s add a second object type of “Height”, with an instance variable called “headHigh”

We would DECLARE the java method like this:

void dropApple(Speed incomingSpeed, Height incomingHeight) {

…. do stuff

}

and call it like this

newton.dropApple(velocity, headHigh);

Objective-C would be declared like this:

(void)dropApple:(Speed *)incomingSpeed dropHeight:(Height *)incomingHeight {

…. do stuff

}

and called like this:

[newton dropApple:velocity incomingHeight:headHigh];

Here’s my first irritant, Objective-C selectors use named parameters – which I like better than Java, but only on the second and subsequent parameters. The first is identified by the name of the selector. It’s just a mixed-bag inconsistency. I hate inconsistencies in my programming languages.

This same system also helps cause Objective-C messages to tend to be very long.

Java and Objective-C also share another trait, that being if a message/call returns an object, you can then, in turn message/call that object immediately without passing it through an unnecessary intermediary object variable.

Let’s say we’re somewhere inside an object that is running active code – like a program. I can always reference back to myself with the special keyword “self”, so if my program has a property of type Person, and a Person object has a property of type “phoneNumber” and a PhoneNumber has a method that returns a formatted representation of the number, we can get to that formatted string in java like this:

self.getPerson().getPhone().format();

Self being an object with a getPerson() accessor that returns a Person, which in turn has a getPhone() accessor which returns a Phone, which in turn has a format() method to return a pretty string. It’s not uncommon to see these things strung together 4 and 5 levels deep inside Java code, and it’s a bit difficult to read, but convenient, and sometimes a lot better than assigning each step to a new variable, like this:

Person aPerson = self.getPerson();

Phone aPhone = aPerson.getPhone();

String aString = aPhone.format();

How does Objective-C handle this? Like this:

[[[self person] phone] format];

Simpler, right? Yes and No. It’s irritating. Why? Because you have to know, in advance, how many layers deep you are going so that you can put the right number of square brackets on the left. If not, you have to come back later and add them, which really “breaks the stride” of typing. It may be less letters to type, but it takes longer to type.

Ah, but along comes Objective-C 2.0 with a “solution” to this: Dot-Syntax!

Oh, but it isn’t the same as Java’s dot-syntax. Objective-C’s dot-syntax only applies to properties, not selectors. (Actually, properties are selectors, you’re not seeing the actual variable, but these are special) That means you can do this:

self.person;

or

self.person.phone;

but not

self.person.phone.format;

instead it would be:

[self.person.phone format];

Why is this a problem? You don’t always know if something is a “property” or “selector”, and since the IDE (XCode in this case) helps fill this stuff in from it’s calculated list of available options, it promotes a certain natural “coding laziness” by letting the IDE do the work of remembering names for you.

So you start typing “self.pe…” and about that time it suggests “self.person”, so you hit the arrow and continue typing “.ph…” and it breaks in again and suggests .”phone” so you hit the arrow to accept and then you start “.format” and nothing happens, then you realize, “oh, it’s not a property”, so you go back and delete the period. That’s bad typing technique. And, then, of course, you have to go back to the beginning of the clause to add a “[" and the end of the line to add "]“

I know, I know. “Gripe, gripe, gripe, gripe”

Next time (probably) “Why there isn’t a consistent method for delegates, actions and ad hoc delegates”

Share

Fate is Determined – Primeval, Series 3, Episode 7 – Review, Spoilers

Worms? What can of worms?

Synopsis

In the middle ages (14th, I think) a “dragon” menaces a village. Sir William goes forth to do battle and we see the “dragon” to be a dinosaur.

In the 21st Century, an anomaly opens up in a car wrecking yard and out comes a dracorex hogwartsia (no, I’m not making that up.) The terrified crane operator fights back and nearly kills the dracorex, which is already wounded with a lance in its side.

Meanwhile, Conner discovers Rex up for auction on eBay and contacts Abby’s good-for-nothing-plot-complication-of-a-brother Jack and tells him to get rex back. Keeping the incident a secret from Abby.

Better late than never, the anomaly detector notices the anomaly and alerts the crew, who arrive just in time to save the dracorex. They try to heard it back through – not seeming the slightest bit interested in how a manmade artifact is imbedded in its side – the anomaly, and just as they are about to succeed, Sir William charges through the anomaly in pursuit of the dracorex, which runs away with Sir William in pursuit.

While Quinn and Conner pursue the knight, Abby and Becker track down the dracorex.

The knight is causing all sorts of problems, but, coincidentally, there’s a carnival parade through the streets of London, so he doesn’t quite stick out like a sore thumb. Quinn and Conner catch up to him several times, but he repeatedly escapes them.

Abby and Becker catch up with the dracorex, and Becker wants to shoot it, but Abby threatens to shoot him with a tranquilizer dart if he does. The beast finally collapses rendering the Mexican standoff moot and they haul the beast back to the junk yard where Abby tries to nurse it back to health.

While all this is going on, Sarah Page decides to trick her way through the anomaly, where she does field research on Sir William. She lies about her authority to pass through the anomaly, and then demands that the guard pretend he saw nothing when she comes back.

Using her knowledge of Sir William, and the extremely fortuitous placement of Sir William’s grave, Sarah convinces him to return to the anomaly. He then battles it out with Abby, who seems to be the only one man enough to take him on, to save the dracorex’s life. He defeats her, but her willingness to sacrifice herself wins him over and he departs, leaving the dracorex behind. Another pet for Abby, it seems.

Jack fails to get Rex back, so Conner takes Becker and some armed soldiers to Jack’s friend and retrieves Rex.

Analysis

Ever since this series started, there’s been the “new” concept that the anomalies have opened in the past, giving rise to various mythological legends. It was that information that allowed Cutter to devise his map of the anomalies, which was verified to be accurate, therefore it was just a matter of time before that showed up in the plot, but this opens a huge can of worms.

Destiny, fate, pre-determinancy and all that baggage that goes along with time travel stories. We’ve mentioned this before, but it’s time for a refresher: Either the past is fixed or it isn’t. Conversely, since the future is someone’s else’s past, the present is either fixed or it isn’t.

Primeval as followed the notion that the past is not fixed, hence the need to return as many creatures through the anomalies as possible. It’s also the reason that Cutter’s world disappeared and was replaced by the current universe and lead to the non-existing of Claudia Brown and replacement by Jenny Lewis. The anomalies represent a very real and incredibly far-reaching threat for the very fabric of reality. Frankly, the ARC doesn’t take this seriously enough. There is nothing more important in the world than stopping the anomalies. It is simply a matter of time before the universe gets changed again – and yet, the people working at the ARC seem to have no grasp that their very existence hangs on a thread that could be severed with the opening of the next anomaly.

A commenter noted earlier that Jenny Lewis would never quit her job just because she nearly died. Could someone walk away – no matter what the reason – from the single most important job in the world? The lives of billions of people, plus billions, if not trillions of plants and animals throughout all time are threatened and the ARC is the only place (we know of) that is combating them. There’s a lot of pressure in a job like that, but it isn’t one that anyone with a conscience could walk away from.

But now we have a new wrinkle. Sir William left his time – he was no longer there to get married and die and be buried. When he left, time would have changed and his grave would be gone – unless it was pre-destined that he would return to the past. If that’s the case, then time can’t be changed and history is safe – but we know it isn’t because of Claudia/Jenny.

So, the next question is, what about the second anomaly in the 14th century? You know, the one that must be there letting the dracorex through in the first place? Keeping with Cutter’s map/theory those events are now part of historical fact – and always have been. They apparently did no lasting damage to the fabric of time. (Although, how would we know it if it had?) If these anomalies have been opening throughout human history, can we not also assume that they’ve been happening throughout the entire 4.6 billion year history of Earth? Are Permian creatures walking through anomalies into the Cretaceous? And why is it that creatures seem to come through the anomalies to our time, but for the most part, things in our time don’t go into the anomalies? If an anomaly opened on a farm in Surrey, would cows be just as likely to walk through into the past as a dinosaur would into the present?

Should we be wondering if anomalies are more one-way than another? Consider, they’re highly magnetic – hold up a spoon and it shoots into the anomaly. What’s it do on the other side? Shoot back through? If not, what does that mean? What would have happened to Sir William in that armor suit of his?

Oh, wait, they’re anomalies they don’t have to behave in a consistent fashion.

Let’s turn to the ARC team then and their procedural operations. Can we at least assume that they keep all their needed gear in their vehicles, ready for a scramble? If so, why don’t they get it out of the car when they go to investigate? They always seem to arrive completely unprepared.

Clearly, from the way Dr. Page has to trick her way through the anomaly, the ARC has standing orders not to go through. That’s probably a good idea when it could lead to the destruction of time. In that case, shouldn’t they also have a standing rule to put creatures back through the anomaly? Wouldn’t Becker know that? Why was he planning on shooting the dinosaur? Would Abby have really shot him with the tranquilizer dart? Does she realize (and as a zoologist, she should) that a dosage big enough for the dracorex would surely kill Becker?

Speaking of Dr. Page going through the anomaly. First she lies to the guard, telling him she has authorization, then on the way out she tells him that he never saw her. Shouldn’t that have been a tip off that she BSed her way in in the first place? Shouldn’t he report that to someone? At least she showed enough curiosity to want to defy orders to go through the anomaly and research. That kind of spirit of inquiry is completely missing in everyone else on the team.

Looks like the end is heating up next week – an expedition into the future to see the aftermath of the destruction of the human race. (Unfortunately, it looks like they’ll be chasing Abby’s good-for-nothing-plot-complication-of-a-brother Jack.)

Share