• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.10.1/pyobjc-45/2.6/pyobjc/pyobjc-framework-Cocoa/Examples/Foundation/Scripts/
1#!/usr/bin/env python
2# This is a doctest
3"""
4==============================
5Using Cocoa collection classes
6==============================
7
8Cocoa contains a number of collection classes, including dictionaries and
9arrays (like python lists) in mutable and immutable variations. We'll
10demonstrate their usage using the ``NSMutableDictonary`` class.
11
12We'll start by importing everything we need::
13
14    >>> from Foundation import *
15
16Then create an empty dictionary::
17
18    >>> d = NSMutableDictionary.dictionary()
19    >>> d
20    {}
21    >>> isinstance(d, dict)
22    False
23    >>> isinstance(d, NSMutableDictionary)
24    True
25
26You can add a new value using the Objective-C API::
27
28    >>> d.setObject_forKey_(42, 'key2')
29    >>> d
30    {key2 = 42; }
31
32But can also use the familiar python interface:
33
34    >>> d['key1'] = u'hello'
35    >>> d
36    {key1 = hello; key2 = 42; }
37
38The same is true for fetching elements::
39
40    >>> d['key2']
41    42
42    >>> d.objectForKey_('key1')
43    u'hello'
44"""
45import doctest
46import __main__
47doctest.testmod(__main__, verbose=1)
48