Welcome to Extelligent Cocoa!

April 2011: I have finally identified the main reason for my frustration with the process of learning how to program Cocoa applications: most resources appear to cater to procedural learners. This has left me – a Gestalt learner – frustrated and wondering how *anyone* could learn like this; with hindsight I must admit that there probably *are* people who learn like that, I’m just not one of them. I’ve felt excluded from a community that mostly says ‘this is what you need to learn’ without considering that maybe I need to learn something completely different first, so one of the things I’ll be doing on this blog is to provide a completely different approach.

What you will find here:

  • Examination of resources available for Cocoa programmers
  • Talk (ok, rantage) about the ‘programming is insanely difficult’ culture
  • Discussion of learning styles and how to develop better resources for Gestalt learners
  • Adventures in coding where I chronicle my personal journey
  • The Virtual Jungle, or rather, -Zoo. Instead of piecemeal applets like Hello World and calculators I plan to be talking about the programming challenges involved in an application of real-world complexity. I’ve settled on programming a virtual zoo because you can show both simple and high-level problems, and since no example exists, I’ll have to _develop_ it. Following examples is very well; but the step from there to creating proper applications is largely uncharted territory.
  • A resource Wiki. More often than anything else, I find myself looking for answers to ‘how do I [open a window/add an image from a filepath/use a dropdown menu with Core Data]‘. Some of these answers take days to hunt down… and they shouldn’t. I want to put them in a public place so that the next time I look for something I’ve already worked out, I’ll find it; and I hope it will be useful to others, too.
  • Snippet Posts. I am still dithering how to implement this feature. I would prefer to keep them off the front page, and have experimented with custom post features; but right now those don’t work very well (no honouring the ‘more’ tag, title does not get turned into a post link, hence no ability to address a post directly), and I’m thinking of folding them back together after all – I already spent an insane amount of time trying to figure it out, and it’s not really worth the hassle.

Unless things change drastically, this blog will be Objective C only. I’m sure there are weird and wonderful things you can do with Java, or by mixing Objective C and other variants, but life is complicated enough that I don’t want to complicate it further.

Posted in EC meta | Leave a comment

Zombie File Found

In users/library there is a folder with the name of my application that contains appname.storedata

I have no idea why it’s there, and why it and *only* it is there, but that was the file that created the problem.

However, it turns out that under Snow Leopard you can no longer search for a document of type x – a search for .storedata turns up zilch. Tiger’s Spotlight app, which I liked an awful lot, has been folded into the general folder search – no more grouping of results (which was useful), no more search by predicate of ‘find me something of the same type as the document I’ve just dragged here’ which Sherlock used to do. Dear Apple: why are we getting less functionality???

So I’ve found one zombie file, but have no idea whether I can find and zap all of them.

Onwards and upwards. And now to make a bloody tree controller work when every single tutorial I find is giving me something that throws exceptions left, right, and centre.

This includes Apple’s own, so I’m not singling anyone out, I am just *so tired* of not even being able to RTFM because there IS no FM that describes how to make the code work in simple steps, and Apple seems to be changing everything around and mixing it up and while I’ve specified in my app delegate that I want my app to store its data in SQLite format, I have no idea what .storedata IS.

Posted in Culture of Programming | Tagged , | Leave a comment

How to make people give up programming

- Update your Development Environment.
- break 90% of your samples to that they either don’t compile because your users are using a version of the environment that is too new, or a version that is too old with a couple of things in the middle. (I’m on Snow Leopard and Xcode 4… which is a reasonable combination and I expect it to be supported).
- sneak in undocumented autosave so that every mistake gets nailed down without options to undo. (I can’t be the only person ever who’ll try something, find it doesn’t work and try to revert to the previous, working, version… which no longer exists.)
- break basic features in a way that is undocumented, frustrating, counterintuitive, and that takes hours to resolve.

What was my crime?

Really lousy design (by Apple) behind cut

Posted in Culture of Programming, Uncategorized | Tagged , | Leave a comment

Xcode 4 samples

It appears that Apple is proving a good many samples for Xcode 4 users.

I needed Richard Wentk’s excellent book (entitled simply Cocoa) to find them.

They are interwoven with the Developer documentation. That’s the documentation you can only access when you’re online, remember?

If you’re not accessing it from a browser but from inside Xcode, and browse the Mac OS X Core Library, you find that it contains a lot of examples, which can be downloaded and opened.

(And I am *so not impressed* that half the links have been updated and are getting redirected to the main page so that the samples aren’t easily accessible after all.)

I liked the old way – where you could download documentation and samples in a version that would be guaranteed to run on your computer/version of Xcode – much better. If I hadn’t happened to glance at the right page in the book – and who in their right mind reads up on how to use documentation – I would have missed this feature for some time yet.

So now I’ve downloaded sample code for the things that interest me most – including NSCollectionView – and can start, dodgy internet connection or not, but argh. Life shouldn’t be so difficult!

Posted in Culture of Programming | Tagged | Leave a comment

C Structures and Enum

(This is another poin where I’m trying to work through a coding issue. I understand how structures work, I’m shaky on when and why you would declare them, what exactly you would do with them, how this fits into the greater landscape of creating objects and how the hell this plays with Core Data.

typedef enum { Domestic, Water, Land, Display } EnclosureType;
enum struct, and a question about usage

Posted in Virtual Zoo, Wiki | Leave a comment

Sticky landing

After a lot of hard drive reorganisation, I have now upgraded to XCode 4. Since the installer is 4.5 GB and it demands 10GB free hard drive space, this was not a trivial undertaking.

I then went and deleted most of the iPhone stuff, leaving the system version that I’m running and the latest; which saved me about 4GB back.

(As usual, things are complicated. I’m not working on the latest system version; or the latest version of Xcode. And right now most of the materials I have refer to Tiger/Xcode 2.5; with the later ones optimised for Xcode 3.2… Xcode 4 is a different beast, and I’m havinng to learn it all over again.)

Xcode 4 does not come with documentation. None, zero, zilch, nothing. It also doesn’t come with more than three examples.

This is so lousy that I don’t have words for the lousyness of this arrangement.

Inclusiveness

Posted in Culture of Programming | Tagged , , | Leave a comment

The Snow Leopard Has Landed

I’ve upgraded my main OS from Tiger to Snow Leopard – I will continue to dual boot, but this means that I’ll have more opportunities to dabble in Xcode, and it also means that I’m moving wholly to garbage-collected code. It is the future after all.

This means I need to relearn some things, I need to tackle the basics again, and I am refamiliarising myself with the examples.

So imagine the wonderful surprise I got when I realised that most of Apple’s examples… *need the OS 10.4 SDK kit installed to run*.

Yep, you read that right. Snow Leopard’s Development tools don’t come with their own set of fully working example applications.

I am not impressed.

Am even less impressed by 2409 Errors stopping TextEdit from running.

No, really. I did not make up that number, 2409 fatal errors in an example that went with _Apple’s own software examples_. Really, folks. You can do better than that.

Posted in Uncategorized | Leave a comment

Resources: Booklisting

http://www.igor-delovski.iz.hr/forum/viewtopic.php?p=3785

has a very long list of Cocoa (OS X/iPhone) books. It’s an exceedingly comprehensive list; many of these are books I hadn’t even heard of and wouldn’t have known how to look for them. Some of them just stand on their own, some have brief reviews, but if you want a bookshelf to browse, this is hands-down the best I have seen.

Posted in Uncategorized | Leave a comment

Encapsulation: the First Principle

Encapsulation isn’t a just a feature of Cocoa, it is shared by all object-oriented programming. It is so fundamental that it’s worth dragging it into the spotlight before talking about anything else. The principle of encapsulation means that you are keeping things separate so you can make changes – sometimes substantial ones – to the individual parts without breaking the application.

Major areas where encapsulation plays a role:

  • Model/View/Controller
  • Objects
  • Accessor Methods
  • Data Encapsulation

Details and Examples

Posted in Application Development, Virtual Zoo | Tagged | Leave a comment

Procedural vs. Gestalt: A declaration of intent

In the past week, I’ve come to the realisisation that I’ve banged my head against walls for a year totally sucking at programming and being made to feel stupid over and over for not understanding even ‘simple’ things _because all the learning materials I’ve had access to were targetted at people with a different learning style_.
drawing conclusions from that insight

Posted in Culture of Programming, EC meta, Teaching Programming | Tagged , , , , | Leave a comment

Starting Programming: The Gestalt Approach

It’s been more a year since I started seriously engaging with Cocoa programming (and no, it wasn’t a continuous process). In that time I’ve read multiple books, glanced at several others, read and followed multiple tutorials, and generally felt very frustrated with the process.

This morning I had another ‘I could have had this easier’ moment that makes me want to scream with frustration: I reached ‘Adding Behaviour to a Cocoa Program‘ in Apple’s Cocoa Fundamentals Guide.

This section covers the question of what programmers are trying to do, and answers it in a conceptually different form: Instead of incorporating library code into your program [as you would in a procedural C application], you incorporate your program code into the framework.

Conceptual Framework for Cocoa Programming

Posted in Application Design, Teaching Programming | Tagged , , , | Leave a comment

Text Handling Overview

I’m posting this as a mainstream post instead of merely as code snippets because I want to talk about the _conceptual_ side of text-in-Cocoa. Apple might have a long and detailed guide but, as usual, it helps me to work these things out myself.

Firstly, _storing_ text is relatively simple and has low overheads. Text files are small compared to image files, video, music, and whatever.
_Displaying_ text, on the other hand, takes up a fair amount of computing power, because in a modern OS the text is transformed into glyphs – vector files that determine the shape of each letter – and then displayed. Back in the day of bitmap display fonts, the overheads were lower (and the displays much uglier).

Text handling falls into three broad categories:

  • Manipulating text strings (including storage)
  • Text Attributes
  • Displaying Text

Content, Attributes and Layout

Posted in Uncategorized, Wiki | Tagged , , , , | Leave a comment

The Book Not Reviewed

Marcus S. Zarra, Core Data

I’m not reviewing it for three reasons: a), right now I can’t follow the examples very far because they are OS X.5 based, b), many of the items the book talks about are beyond the scope of things I’m interested in right now (though it was good to learn what’s ahead in terms of version control and data migration etc), and c) the author’s attitude on this forum thread annoyed me. “You need to become a better programmer” is not a sufficient answer to ‘your material is hard to understand and you’re leaving out explanations’. (The title of the thread will explain how I came to find it; Zarra’s use of ‘text area [p.20]‘ to refer to what looks like an NSTextView [illustration p.18] _but which is not referred to in the text otherwise_ is, to put it mildly, sloppy, and anyone who can’t follow that instruction does not need more programming experience, they need better explanations.)

Posted in Culture of Programming, Teaching Programming | Tagged , | Leave a comment

NSTextView and Core Data: why do we even ask?

Last year, when I first discovered CoreData, I experimented a little, but failed at binding an NSTextView to Core Data.
Set Core Data Value[1827] *** NSRunLoop ignoring exception 'Cannot create NSData from object newTag of class NSCFString' that raised during posting of delayed perform with target 3c1890 and selector 'invokeWithTarget:'

I am not certain what exacly the hidden assumptions _are_ in this context, but there must be some, because the explanation of what is going on proved to be exceedingly simple and exceedingly difficult to find.

The problem… and the problem with the solution

Posted in Culture of Programming | Tagged , , , | 2 Comments

Six meta-skills for new programmers

I’ve been thinking about the intended audience of so many teaching materials, and of how this leaves out what I consider to be one of the larger groups of potential programmers – people who know Macs and how Mac applications should work, who have some experience with application development (scripting, web applications) but no actual programming background. I might or might not write that up in greater detail, but I thought I’d look at the topic from the other side. In my opinion, every new programmer should learn

6 Things make a post

Posted in Teaching Programming | Tagged , , , , , | Leave a comment

Resources: A Friendlier Approach

I wish I had found this article when I first started to learn about Cocoa, I could have saved myself a lot of frustration – not because the books I’ve talked about are any less frustrating, but because someone GETS IT and gives an approach for learning Cocoa that differs from the mimetic of most books and tutorials.

http://andymatuschak.org/articles/2007/09/09/getting-started-with-cocoa-a-friendlier-approach/
(and read the comments as well, some of the good stuff happens in the comments.)

The short form of that article is ‘learn how Cocoa works! understand what it does! learn how to solve problems! Don’t get distracted by someone else’s shiny code!’

I haven’t actually worked through this yet. At some point in the not-too-far future I’ll have to jump into the deep waters of actually clawing my way through functions and classes to write some working code – I will need leisure for this, because it Will Go Wrong, but for now, I feel that I’m starting to get a feel for what I’m doing… and I am exceedingly glad that I am not alone.

Posted in Culture of Programming, Resources, Teaching Programming, Tutorials | Tagged , | Leave a comment

Resource: Apple Developer Documentation

Several of of Apple’s guides are available – free – in the iBook store, so I am currently reading the Cocoa Fundamentals Guide on my iPhone. I’m on page 106 of 699, and so far, it’s been a very useful read. Yes, I *do* know some – most – of these things already, but they’re put together in a form that makes it easier to understand how they all hang together.

For the moment, I am going with the ‘read as much as I can’ approach – it doesn’t help me to write code, but I feel much less overwhelmed by the code I encounter. Making sure I understand _everything_ is very timeconsuming, but it should prevent me from making fundamental mistakes later on, because copying other people’s half-digested code out of context is *not* a route to programming success.

Posted in Resources | Tagged , | Leave a comment

Cocoa Font Attribute Handling

I am currently working through _David Chisnall: Cocoa Programming Fundamentals_ because it gives me a chance to engage with various Cocoa topics. I’ll write a detailed review once I am finished, but it’s a very frustrating resource: firstly, because the author uses a lot of shortcuts and very specific code that only serves to make it harder to understand what is going on on an abstract level, and second, because it’s full of hidden assumptions. One of them is the assumption that readers will somehow know how font styles are handled in Cocoa.

I’ve described the actual mechanics of setting a font attribute (font family) in this snippet, but it was, of course, too much to hope that I could actually use that example to understand font attribute handling in general…

Conceptual Background

Posted in Culture of Programming, Teaching Programming | Tagged , , , , | Leave a comment

Tutorials: UR doing it right

In looking for the solution for a particular problem – loading more than one .nib file – I came across the following article:

http://www.gentlebytes.com/2009/09/coredata-bindings-multiple-nibs/

Disclaimer: I haven’t actually _done_ this tutorial yet, so I don’t know whether it works, but I would like to point out the many ways in which it this tutorial is paying extra attention to eliminating potential sources of confusion and how it places this simple technique in a wider programming context.

Fourteen reasons to like this article

Posted in Culture of Programming, Resources, Teaching Programming, Tutorials | Tagged , | Leave a comment

Eschew Obfuscation

A friend of mine commented that points[pointCount++ % 4] = click qualifies for ‘obfuscated C’ (as in the obfuscated C contest), and they have a good point: the code may be very clever, but it’s also rather dense; and especially in a learning resource, not appropriate. (Others may comment on how much difference it makes to actual applications and compilation times – apparently very little.)

The next example from the same resource – David Chisnall’s “Cocoa Programming Fundamentals” contained

for (unsigned y=0 ; y<2 ; y++);

. This does not compile in my older XCode version, but at least I was able to unravel what he was doing and dissolved it into
int y;
for (y=0 ; y<2 ; y++)

which did.
(I'm assuming that he's using 'unsigned' because the counter cannot be negative in this context and there's a slightly lower memory overhead?)

The main problem I see with this is that the code seems to be immensely optimised for _this_ application, _this_ function. This is a loop for writing data in a grid: two rows, seven columns - and was hardcoded, which for an example is acceptable, but I nonetheless want to turn it into

int y;
int noOfRows = 2;
for (y=0 ; y<noOfRows ; y++)

because a hard-coded unexplained number somewhere in the middle of your sample code does not sound like a good idea to me. And sure, this is a *very* brief sample - but people like me are not unlikely to save it to a file somewhere with the note 'this works to do x' and then drag it out again because we know it works and contains no typos, and I can see that when you look at it in a year's time you might need to spent a little more effort to work out where that '2' came from and how you arrived at it, and of course you need to go and find every instance of the right '2' in your code to change - whereas if you'd just used a variable, you only need to change it once.

This strikes me as one of the principles of good programming practice, actually: if you start with a simple function and expand it later - get it working, then tweak it, or get it working, then save it somewhere accessible and reuse it across a number of projects (because why should you work out the same thing over and over again?), keeping the code expandable, using variables with speaking names instead of hard-coded values, and commenting your code whenever you do something unusual (or just to document your logic) seem to me good habits to get into, because one day I will leave the realm of doing Very Simple Things and enter that of doing simple things that interact with each other in clever ways.

I'm coming to wonder whether super-optimised code - that shaves off a fraction of a second here, a line there - doesn't turn into a disadvantage for the kind of thing I'm doing right now - because it seems less flexible, less easy to adapt.

I've got a good mind to go back to points[pointCount++ % 4] = click and pull it apart.

Posted in Culture of Programming, Teaching Programming | Tagged , | Leave a comment

Explain Early, Explain Often

Today I came across a function that I understood on a general level (more details on this snippet. I could see what it does: the user clicks the mouse four times, and the application uses those clicks to draw a Bezier curve: start point, first bezier handle, second bezier handle, end point. After the fourth click,

[self setNeedsDisplay: YES];

is called, and the whole thing is drawn.

Fine, done, move on.

Only… I hadn’t understood how, exactly, the function worked. I hadn’t understood that you could _do_ this with Objective C. The line that got me was

points[pointCount++ % 4] = click;

.
A lengthy explanation for a short line

Posted in Teaching Programming | Tagged | Leave a comment