Ronald Oussoren committed 1d6c0a3

Convert to a doctest

Comments (0)

Files changed (1)


-# Using an NSDictionary.
-# You can use both the Python and Objective-C interfaces to access a
-# dictionary.
-from Foundation import *
+#!/usr/bin/env python
+# This is a doctest
+Using Cocoa collection classes
-obj = NSMutableDictionary.dictionary()
-print "An empty NSMutableDictionary:", obj
-print "Actual type:", type(obj)
+Cocoa contains a number of collection classes, including dictionaries and
+arrays (like python lists) in mutable and immutable variations. We'll 
+demonstrate their usage using the ``NSMutableDictonary`` class.
-print "Setting key2 the objective-C way"
-obj.setObject_forKey_(42, 'key2')
-print "Setting key1 the python way"
-obj['key1'] = 'value1'
+We'll start by importing everything we need::
-print "key1 = ", obj.objectForKey_('key1')
-print "key2 = ", obj['key2']
+    >>> from Foundation import *
-# Print values
-print "all keys:", obj.keys()
-print "all values:",  obj.values()
-print "A filled NSMutableDictonary:", obj
+Then create an empty dictionary::
+    >>> d = NSMutableDictionary.dictionary()
+    >>> d
+    {}
+    >>> isinstance(d, dict)
+    False
+    >>> isinstance(d, NSMutableDictionary)
+    True
+You can add a new value using the Objective-C API::
+    >>> d.setObject_forKey_(42, 'key2')
+    >>> d
+    {key2 = 42; }
+But can also use the familiar python interface:
+    >>> d['key1'] = u'hello'
+    >>> d
+    {key1 = hello; key2 = 42; }
+The same is true for fetching elements::
+    >>> d['key2']
+    42
+    >>> d.objectForKey_('key1')
+    u'hello'
+import doctest
+import __main__
+doctest.testmod(__main__, verbose=1)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.