Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes.</p> <p>For one thing, as you note, all the documentation is written for Objective-C, which is a very different language.</p> <p>One difference is method name. In Objective-C, when you send a message to (Python would say “call a method of”) an object, the method name (selector) and arguments are mixed:</p> <pre><code>NSURL *URL = /*…*/; NSError *error = nil; QTMovie *movie = [QTMovie movieWithURL:URL error:&amp;error]; </code></pre> <p>This isn't possible in Python. Python's keyword arguments don't count as part of the method name, so if you did this:</p> <pre><code>movie = QTMovie.movieWithURL(URL, error = ???) </code></pre> <p>you would get an exception, because the QTMovie class has no method named <code>movieWithURL</code>; the message in the Objective-C example uses the selector <code>movieWithURL:error:</code>. <code>movieWithURL:</code> and <code>movieWithURL</code> would be two other selectors.</p> <p>There's no way they can change this, because Python's keyword arguments aren't ordered. Suppose you have a hypothetical three-argument method:</p> <pre><code>foo = Foo.foo(fred, bar=bar, baz=baz) </code></pre> <p>Now, this calls <code>foo:bar:baz:</code>, right?</p> <p>Not so fast. Foo may also have a method named <code>foo:baz:bar:</code>. Because Python's keyword arguments aren't ordered, you may actually be calling that method. Likewise, if you tried to call <code>foo:baz:bar:</code>, you may actually end up calling <code>foo:bar:baz:</code>. In reality, this case is unlikely, but if it ever happens, you would be unable to reliably call either method.</p> <p>So, in PyObjC, you would need to call the method like this:</p> <pre><code>movie = QTMovie.movieWithURL_error_(URL, ???) </code></pre> <p>You may be wondering about the ???. C doesn't allow multiple return values, so, in Objective-C, the <code>error:</code> argument takes a pointer to a pointer variable, and the method will store an object in that variable (this is called return-by-reference). Python doesn't have pointers, so the way the bridge handles arguments like this is that you pass None, and the method will (appear to) return a tuple. So the correct example is:</p> <pre><code>movie, error = QTMovie.movieWithURL_error_(URL, None) </code></pre> <p>You can see how even a simple example deviates from what documentation might show you in Objective-C.</p> <p>There are other issues, such as the GIL. Cocoa apps are only going to get more concurrent, and you're going to want in on this, especially with tempting classes like NSOperation lying around. And <a href="http://blip.tv/file/2232410" rel="noreferrer">the GIL is a serious liability, especially on multi-core machines</a>. I say this as a Python guy myself (when not writing for Cocoa). As David Beazley demonstrates in that video, it's a cold, hard fact; there's no denying it.</p> <p>So, if I were going to switch away from Objective-C for my apps, I would take up <a href="http://macruby.org/" rel="noreferrer">MacRuby</a>. Unlike with PyObjC and RubyCocoa, messages to Cocoa objects don't cross the language bridge; it's a from-the-ground-up Ruby implementation in Cocoa, with language extensions to better support writing Cocoa code in it.</p> <p>But that's too far ahead of you. You're just getting started. Start with Objective-C. Better to avoid <em>all</em> impedance mismatches between the language you're using and the one the documentation is written for by keeping them the same language.</p> <p>Plus, you'll find some bugs (such as messages to deceased objects) harder to diagnose without knowledge of how Objective-C works. You <em>will</em> write these bugs as a new Cocoa programmer, regardless of which language you're writing the code in.</p> <p>So, learn C, then learn Objective-C. A working knowledge of both shouldn't take more than a few weeks, and at the end of it, you'll be better prepared for everything else.</p> <p>I won't go into how I learned C; suffice to say that I do not recommend the way I did it. I've heard that <a href="http://rads.stackoverflow.com/amzn/click/1430218096" rel="noreferrer">this book</a> is good, but I've never owned nor read it. I do have <a href="http://oreilly.com/catalog/9781565923065/" rel="noreferrer" title="Practical C Programming by Steve Oualline">this book</a>, and can confirm that it's good, but it's also not Mac-specific; skip the chapter on how to compile the code, and use Xcode instead.</p> <p>As for Objective-C: <a href="http://rads.stackoverflow.com/amzn/click/0321503619" rel="noreferrer">The Hillegass book</a> is the most popular, but I didn't use it. (I have skimmed it, and it looks good.) I read <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/" rel="noreferrer">Apple's document on the language</a>, then jumped right in to writing small Cocoa apps. I read some of <a href="http://developer.apple.com/mac/library/navigation/" rel="noreferrer">the guides</a>, with mixed results. There is <a href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCTutorial/" rel="noreferrer">a Currency Converter tutorial</a>, but it didn't help me at all, and doesn't quite reflect a modern Cocoa app. (Modern apps still use outlets and actions, but also Bindings, and a realistic Currency Converter would be almost entirely a couple of Bindings.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload