<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mediawiki.gnustep.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Newacct</id>
	<title>GNUstepWiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://mediawiki.gnustep.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Newacct"/>
	<link rel="alternate" type="text/html" href="https://mediawiki.gnustep.org/index.php/Special:Contributions/Newacct"/>
	<updated>2026-06-16T10:23:15Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.35.7</generator>
	<entry>
		<id>https://mediawiki.gnustep.org/index.php?title=Creating_document_based_applications&amp;diff=6117</id>
		<title>Creating document based applications</title>
		<link rel="alternate" type="text/html" href="https://mediawiki.gnustep.org/index.php?title=Creating_document_based_applications&amp;diff=6117"/>
		<updated>2011-02-03T21:17:58Z</updated>

		<summary type="html">&lt;p&gt;Newacct: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Note: First version; subject to further revisions and changes.&lt;br /&gt;
--[[User:Quineska|ChrisArmstrong]] 07:09, 7 Jul 2005 (CEST)&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
[[OpenStep]] supports document-based applications, simplifying the process of creating applications whose behaviour is familiar to many users. [[GNUstep]] also provides this functionality to the developer. This guide aims to provide an overview of what is required in the creation and design of such an application.&lt;br /&gt;
&lt;br /&gt;
It is assumed you are familiar with GNUstep, and have some understanding as how to create [[Gorm]]'s interface files (often referred to as nibs), as well as some understanding of how they work.&lt;br /&gt;
&lt;br /&gt;
Importantly, such applications have certain components that are required, and should be considered as part of their design. When programming, classes that will be used as part of the [[AppKit]] are:&lt;br /&gt;
&lt;br /&gt;
==== NSDocument ====&lt;br /&gt;
&lt;br /&gt;
This class is central to the OpenStep document based model. These application's declare a new instance of a subclass of NSDocument for each “document” that is opened. You will subclass NSDocument to implement this.&lt;br /&gt;
&lt;br /&gt;
Each instance of your NSDocument subclass may have one or more windows associated with it. These are used as an interface for loading and saving a representation of your document type. Each window will have an “NSWindowController” instance associated with it, where NSDocument maintains a list of these.&lt;br /&gt;
&lt;br /&gt;
==== NSDocumentController ====&lt;br /&gt;
&lt;br /&gt;
This class acts a controller. It maintains a list of documents (instances of your NSDocument subclass) and is responsible for loading and instantiating new documents. It is usually not necessary to implement a subclass of this, but often it may be useful to implement special functionality (especially related to opening and creating documents at an application level).&lt;br /&gt;
&lt;br /&gt;
==== NSWindowController ====&lt;br /&gt;
&lt;br /&gt;
As mentioned above, NSWindowController instances are individually associated with one NSWindow instance, in such a way that a document maintains a list of window controllers that are responsible for rendering the views associated with a document.&lt;br /&gt;
&lt;br /&gt;
You will most likely not have to subclass NSWindowController. Instead, you associate a nib file (the Gorm output) with the subclass of NSDocument, and NSDocumentController will load this nib file for each document that is created or loaded.&lt;br /&gt;
&lt;br /&gt;
For example, a text based application that is used for the editing of plain text files, may subclass NSDocument with a TextDocument class. Each instance of this class would have an NSWindowController instance associated with it, which in turn manages a window instance that is instantiated from a nib file. The NSDocumentController instance, as managed and instantiated by GNUstep, would be responsible for loading and saving documents in your application. It even prompts the user to save their documents when they try to quit the application.&lt;br /&gt;
&lt;br /&gt;
== File Components ==&lt;br /&gt;
&lt;br /&gt;
In building your application you will need to create a number of special files. From scratch, the following should be a rough guide to getting it working. These include the Makefile, your application's property list and the interface files.&lt;br /&gt;
&lt;br /&gt;
=== The Makefile ===&lt;br /&gt;
&lt;br /&gt;
For this type of application, no special makefile is needed: it just has to be a normal application. Project Builder should be able to spit out the required makefile and Gorm files that are needed for a generic application. I believe it is also capable of providing support for document-based applications. Otherwise use the following as a template:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''GNUmakefile'''&lt;br /&gt;
&lt;br /&gt;
 include $(GNUSTEP_MAKEFILES)/common.make&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
 APP_NAME = DocumentApp&amp;lt;br&amp;gt;&lt;br /&gt;
 DocumentApp_OBJC_FILES =&amp;lt;br&amp;gt;&lt;br /&gt;
  MyDocument.m &amp;lt;br&amp;gt;&lt;br /&gt;
  main.m&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
 DocumentApp_RESOURCES = \ &amp;lt;br&amp;gt;&lt;br /&gt;
  Resources/Info-gnustep.plist \&amp;lt;br&amp;gt;&lt;br /&gt;
  Resources/DocumentApp.gorm \&amp;lt;br&amp;gt;&lt;br /&gt;
  Resources/MyDocument.gorm&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
 -include GNUmakefile.preamble&amp;lt;br&amp;gt;&lt;br /&gt;
 include $(GNUSTEP_MAKEFILES)/application.make&amp;lt;br&amp;gt;&lt;br /&gt;
 -include GNUmakefile.postamble&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For more information as to customising this file, as well as setting up compiler options for third party libraries and includes, see the GNUstep makefile manual.&lt;br /&gt;
&lt;br /&gt;
===The application dictionary===&lt;br /&gt;
&lt;br /&gt;
For those that have edited this file in their project before, it is a dictionary with various values entered. For our purposes, we need an array within the main dictionary, called &amp;quot;NSTypes&amp;quot;. This is a array of unnamed dictionaries, with one dictionary for each type. &lt;br /&gt;
&lt;br /&gt;
I have provided an example for this file below. ProjectBuilder creates one as well. Please note that &amp;quot;TextEdit&amp;quot; does exist as an example application from NEXT I think, and again on MacOS X but I have constructed a much simpler version, used only for plain text files.&lt;br /&gt;
&lt;br /&gt;
Taking a look at the single dictionary entry in the NSTypes array, the following key-value pairs are needed:&lt;br /&gt;
* '''NSDocumentClass:''' This is (string) the subclass name you use in your code, the &amp;quot;objective-c name&amp;quot; of your document subclass. This is used by NSDocumentController's default implementation to create instances of your document type.&lt;br /&gt;
* '''NSName:''' The generic file type (string). This is completely arbitrary, and can be anything you like. For the purpose of conventions, it may be more appropriate not to use the &amp;quot;NS&amp;quot; or &amp;quot;GS&amp;quot; prefix (the latter of which I not sure), and instead use either your own, or no prefix at all.&lt;br /&gt;
* '''NSHumanReadableName:''' The human readable name of your document type (string)? TODO: explain where this is used.&lt;br /&gt;
* '''NSUnixExtensions:''' An array of strings, each containing a file extension connected to this document type on &amp;quot;unix&amp;quot; platforms. TODO: explain how &amp;quot;unix&amp;quot; is interpreted by GNUstep.&lt;br /&gt;
* '''NSDOSExtensions:''' An array of strings, each containing a file extension connected to this document type on &amp;quot;DOS&amp;quot; platforms (could be loosely interpreted as any MS platforms GNUstep runs on, i.e. Windows 2000/XP).&lt;br /&gt;
* '''NSRole:''' A string, either &amp;quot;Viewer&amp;quot; or &amp;quot;Editor&amp;quot;, depending on how your document operates on this file type.&lt;br /&gt;
* '''NSIcon:''' A icon name associated with this document type. TODO: Investigate how this works.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''Info-gnustep.plist'''&lt;br /&gt;
 {&lt;br /&gt;
     ApplicationDescription = &amp;quot;A simple text editor for GNUstep.&amp;quot;;&lt;br /&gt;
     ApplicationName = TextEdit;&lt;br /&gt;
     ApplicationRelease = 0.10;&lt;br /&gt;
     Authors = ( &amp;quot;Christopher Armstrong&amp;quot; );&lt;br /&gt;
     Copyright = &amp;quot;Copyright (C) 2005 Christopher Armstrong&amp;quot;;&lt;br /&gt;
     CopyrightDescription = &amp;quot;Released under GPL.&amp;quot;;&lt;br /&gt;
     FullVersionID = 0.10;&lt;br /&gt;
     NSExecutable = TextEdit;&lt;br /&gt;
     NSMainNibFile = TextEdit.gorm;&lt;br /&gt;
     NSPrincipalClass = NSApplication;&lt;br /&gt;
     NSRole = Application;&lt;br /&gt;
     NSTypes = (&lt;br /&gt;
         {&lt;br /&gt;
                 NSDocumentClass = &amp;quot;TextDocument&amp;quot;;&lt;br /&gt;
                 NSName = &amp;quot;GSTextDocumentType&amp;quot;;&lt;br /&gt;
                 NSHumanReadableName = &amp;quot;Text Document&amp;quot;;&lt;br /&gt;
                 NSUnixExtensions = ( &amp;quot;txt&amp;quot;, &amp;quot;&amp;quot; );&lt;br /&gt;
                 NSDOSExtensions = ( &amp;quot;txt&amp;quot; );&lt;br /&gt;
                 NSRole = Editor;&lt;br /&gt;
         }&lt;br /&gt;
     );&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For more information on property list files, consult [[Property Lists]].&lt;br /&gt;
&lt;br /&gt;
=== Interface files ===&lt;br /&gt;
&lt;br /&gt;
Two interface files are needed: one for your main application, and another that will be instantiated with each instance of your document subclass. These both should be listed as resources in your makefile, (as shown the GNUmakefile example above). It may be helpful to name the document nib file after your class.&lt;br /&gt;
&lt;br /&gt;
==== Application Interface File ====&lt;br /&gt;
&lt;br /&gt;
The application one should just be a menu (though it could be more if necessary). When constructing it, take the following into account:&lt;br /&gt;
&lt;br /&gt;
* '''Use an &amp;quot;Application&amp;quot; template''': Use a standard Gorm template for an Application.&lt;br /&gt;
* '''Delete the NSWindow instance''': In the &amp;quot;Objects&amp;quot; pane, remove the NSWindow instance by going File-&amp;gt;Delete (unless you need a main window, which is beyond the scope of this document).&lt;br /&gt;
* '''Set NSOwner's class to &amp;quot;NSDocumentController&amp;quot;''': This is the custom class option of NSOwner in the property inspector. This ensures that the openDocument:, newDocument: messages, as sent to NSFirst, get picked up by NSDocumentController. Do not instantiate it (I believe you can't anyway).&lt;br /&gt;
* '''[Optional] Subclass NSDocumentController''':If you intend to subclass NSDocumentController, subclass it in the Classes pane. To make sure that this class is instantiated and used, make sure you instantiate it in Gorm or in your main.m file, and then set NSOwner to be of your custom subclass type.&lt;br /&gt;
* '''Add a &amp;quot;Document&amp;quot; menu''': Drag a Document menu from the palette onto your main menu. This already has the proper outlet and action linkage for each relevant message to be directed to NSFirst. Generic messages (such as openDocument:, not associated with any document instance) are forwarded to the shared instance of NSDocumentController (see it's reference documentation). When a document is active, more specific messages (such as close: or saveDocument:) are forwarded to the relevant document instance of your NSDocument subclass.&lt;br /&gt;
* '''[Optional] Add other menus''': Add any appropriate menus such as &amp;quot;Info&amp;quot; and &amp;quot;Format&amp;quot; which may be relevant to your application.&lt;br /&gt;
&lt;br /&gt;
==== Document Interface File(s) ====&lt;br /&gt;
&lt;br /&gt;
The document interface file is a window containing the view(s) required for the implementation of one instance of your document type. For example, in the case of a TextDocument type, a window instance with a NSTextView drawn on it is used for each document window. This interface file is instantiated every time a person creates a new document (the newDocument: message is sent to NSDocumentController) or opens an existing document (openDocument:).&lt;br /&gt;
&lt;br /&gt;
You will need to:&lt;br /&gt;
* '''Subclass NSDocument''' with your document type in Gorm. This is done in a couple of ways, see [[Gorm_Manual#Custom_classes|custom classes]]. It is up to you whether or not you let Gorm generate the class files, but make sure you add the appropriate outlets to your class in the source files (otherwise, I think your your program may segfault).&lt;br /&gt;
* '''Set NSOwner:''' Set your subclass as NSOwner's Custom class using the Property Inspector. ''DO NOT INSTANTIATE IT.''&lt;br /&gt;
&lt;br /&gt;
Another thing that you may want to consider is custom outlets. If you place certain views or controls on your window (including the window itself), you may want to refer to them directly in your code, e.g. the NSTextView in our TextEdit example is accessed directly, so that we can save it's contents to a file. To enable this, we add outlets to our class (see [[Gorm_Manual#Custom_classes|custom classes]]). We then connect NSOwner to the appropriate controls/views, and select the outlets in the Connections Property Inspector (see [[Gorm_Manual#Editing_the_interface|editing the interface]]).&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
Various things need to be done at a code level, to ensure that your NSDocument subclass is properly instantiated. This section aims to tell you what is necessary.&lt;br /&gt;
&lt;br /&gt;
=== NSDocument subclass ===&lt;br /&gt;
&lt;br /&gt;
When creating your subclass, you may have chosen to have Gorm create the class files. If not, you should create two files for your subclass: a header (.h) file and an implementation file (.m). The header file should take the following layout (we're using the TextEdit example again):&lt;br /&gt;
&lt;br /&gt;
 #ifndef TEXTDOCUMENT_H&lt;br /&gt;
 #define TEXTDOCUMENT_H &amp;lt;br&amp;gt;&lt;br /&gt;
 #include &amp;lt;AppKit/AppKit.h&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
 @class NSDocument; &amp;lt;br&amp;gt;&lt;br /&gt;
 /* The document type string, as specified in the property list [[#The_application_dictionary|NSTypes array]] */ &amp;lt;br&amp;gt;&lt;br /&gt;
 NSString * GSTextDocumentType = &amp;quot;GSTextDocumentType&amp;quot;;&lt;br /&gt;
 @interface TextDocument : NSDocument&lt;br /&gt;
 {&lt;br /&gt;
 @protected&lt;br /&gt;
     /* Outlets we added in our Gorm nib file */&lt;br /&gt;
     id * textView;&lt;br /&gt;
     NSString* fileContents;&lt;br /&gt;
 }&lt;br /&gt;
 /* Basic methods used for loading and saving document data (respectively). */&lt;br /&gt;
 - (BOOL) loadDataRepresentation:(NSData*)representation ofType:(NSString*)type;&lt;br /&gt;
 - (NSData*) dataRepresentationOfType: (NSString*)type; &amp;lt;br&amp;gt;&lt;br /&gt;
 /* Information and events handling of GUI related changes */&lt;br /&gt;
 - (NSString*) windowNibName;&lt;br /&gt;
 - (void) windowControllerDidLoadNib:(NSWindowController*)windowController; &amp;lt;br&amp;gt;&lt;br /&gt;
 @end&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Firstly, take note that we inherit from NSDocument. As a result, we need to override some methods for a basic (but working) implementation. &lt;br /&gt;
&lt;br /&gt;
==== loadDataRepresentation:ofType: and dataRepresentationOfType: ====&lt;br /&gt;
&lt;br /&gt;
The methods, loadDataRepresent:ofType: and dataRepresentationOfType: are used by NSDocumentController to load and save our files. The first method is called when the user tries to open a document (NSDocumentController gets the file name and copies it's data using an open panel). The method is passed the raw data in this file, as well as a string representing its type. It is important to note that this type string is the same one used in your property list. It is used by NSDocumentController to tell you what type it thinks you should open the data with. If this is not possible, or if there is an error trying to parse the data, your method returns FALSE/NO. Otherwise, you should store a local, meaningful representation of the data, which you can insert into your document window a little later (and return TRUE/YES).&lt;br /&gt;
&lt;br /&gt;
For example, in terms of the TextEdit example:&lt;br /&gt;
 - (BOOL) loadDataRepresentation:(NSData*) representation ofType:(NSString*)type&lt;br /&gt;
 {&lt;br /&gt;
    /*Check type information */&lt;br /&gt;
    if (![type isEqual:GSTextDocumentType])&lt;br /&gt;
        return NO;&lt;br /&gt;
    /* Clean out our old representation if it still exists */&lt;br /&gt;
    if (self-&amp;gt;fileContents) RELEASE(self-&amp;gt;fileContents); &amp;lt;br&amp;gt;&lt;br /&gt;
    /* Allocate room for the new data, and try to initialise a string with it. */&lt;br /&gt;
    self-&amp;gt;fileContents = [NSString alloc];&lt;br /&gt;
    self-&amp;gt;fileContents = [self-&amp;gt;fileContents initWithBytes: [representation bytes]&lt;br /&gt;
                                                    length: [representation length]&lt;br /&gt;
                                                  encoding: NSASCIIStringEncoding]; &amp;lt;br&amp;gt;&lt;br /&gt;
    /* If this can't be done, return NO */&lt;br /&gt;
    if (!self-&amp;gt;fileContents)&lt;br /&gt;
        return NO; &amp;lt;br&amp;gt;&lt;br /&gt;
    /* Otherwise, set our TextView to the string data. */&lt;br /&gt;
    [self-&amp;gt;textView setString: fileContents];&lt;br /&gt;
    return YES;&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If this is not sufficient, or if your program can open the data in a more efficient manner, it may be worth looking into overriding -initWithContentsOfFile:ofType: and -initWithContentsOfURL:ofType:. Again, see the GUI manual for more details.&lt;br /&gt;
&lt;br /&gt;
dataRepresentationOfType: is used to save the data. Your method is expected to return an instance of NSData that contains what should be saved to a file, for the type specified. Again, the ''type'' variable should be checked to ensure that you can write data of that type. Return nil if the file cannot be saved as that type.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
 - (NSData*) dataRepresentationOfType:(NSString*)type&lt;br /&gt;
 {&lt;br /&gt;
     if (![type isEqual:GSTextDocumentType])&lt;br /&gt;
         return nil; &amp;lt;br&amp;gt;&lt;br /&gt;
     NSData stringData =  [[textView string] dataUsingEncoding:NSASCIIStringEncoding];  &lt;br /&gt;
     return stringData; &lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
If a more sophisticated save implementation is needed, consult the documentation for information on the writeTo* methods.&lt;br /&gt;
&lt;br /&gt;
==== windowNibName ====&lt;br /&gt;
&lt;br /&gt;
This method must be overridden to return the a string of the name of your nib file (without the file extension). More complex implementations will use other methods to load the interface files. TODO: information on what methods need to be overridden.&lt;br /&gt;
&lt;br /&gt;
==== windowControllerDidLoadNib: ====&lt;br /&gt;
&lt;br /&gt;
Use this method to add code that should be run as soon as ''windowController'' has been initialised. Your document may have many window controllers; each window controller taking possession of one window.&lt;br /&gt;
&lt;br /&gt;
In the TextEdit example, this method is used to copy the ''fileContents'' variable to the text view. It otherwise initialises the document window. &lt;br /&gt;
&lt;br /&gt;
Also note that you can override windowControllerWillLoadNib: to perform pre-interface loading initialisation.&lt;br /&gt;
&lt;br /&gt;
==== makeWindowControllers ====&lt;br /&gt;
&lt;br /&gt;
This is an optional method to override, which must be used if you have multiple window controllers per document. It is responsible for creating each window controller from it's interface file and then adding it to the document. Use the addWindowController: method.&lt;br /&gt;
&lt;br /&gt;
==== Other considerations ====&lt;br /&gt;
&lt;br /&gt;
* You need a way to notify NSDocument that your document has changed, so that NSDocumentController can enable the menu item to allow saving. This is done by a call to the updateChangeCount: method. For example, the TextDocument class adds itself as a delegate to the textView, for any notification of changes. It then calls updateChangeCount: in the delegate's method.&lt;br /&gt;
&lt;br /&gt;
* You may also wish to add a close: method (not the close method already in NSDocument, see below) to respond to the menu item permitting your document to close. This could perform cleanup operations before a call to [self close] is made e.g.&lt;br /&gt;
&lt;br /&gt;
 - (void) close:(id)sender /* Note this is not the method found in NSDocument (they differ by a parameter) */&lt;br /&gt;
 {&lt;br /&gt;
     [self close];&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
=== NSDocumentController ===&lt;br /&gt;
&lt;br /&gt;
This class can optionally be subclassed, to implement some specific functionality. For example, programs handling multiple document types may override newDocument: to create a window so that the user can select which document type to create.&lt;br /&gt;
&lt;br /&gt;
You should use separate implementation files and add the .m file to GNUmakefile.&lt;br /&gt;
&lt;br /&gt;
=== NSWindowController ===&lt;br /&gt;
&lt;br /&gt;
This class can also be optionally subclassed, to implement functionality specific to a window controller for a document type. You may need to override makeWindowControllers: in the NSDocument subclass in order to make sure your specific window controller is instantiated.&lt;br /&gt;
&lt;br /&gt;
You should use separate implementation files and add the .m file to GNUmakefile.&lt;br /&gt;
&lt;br /&gt;
== Optional Additions ==&lt;br /&gt;
&lt;br /&gt;
=== Custom NSDocumentController and NSWindowController implementations ===&lt;br /&gt;
&lt;br /&gt;
See [[#NSWindowController|NSWindowController]] and [[#NSDocumentController|NSDocumentController]].&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Category:Development]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>
	</entry>
	<entry>
		<id>https://mediawiki.gnustep.org/index.php?title=HOM&amp;diff=5929</id>
		<title>HOM</title>
		<link rel="alternate" type="text/html" href="https://mediawiki.gnustep.org/index.php?title=HOM&amp;diff=5929"/>
		<updated>2010-06-07T18:38:04Z</updated>

		<summary type="html">&lt;p&gt;Newacct: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;HOM (short for ''High Order Messaging'') is a mechanism for encapsulating control structures and other programming patterns. It is similar to blocks in [[Smalltalk]] and higher order functions in functional languages.&lt;br /&gt;
&lt;br /&gt;
[[Objective-C]] does not have blocks. They're not a part of the language, although [http://virtualschool.edu/cox/ Brad Cox]'s [http://virtualschool.edu/cox/pub/TaskMaster/index.html TaskMaster Paper] outlines them nicely (and the [http://users.pandora.be/stes/compiler.html Portable Object Compiler] implements them).&lt;br /&gt;
&lt;br /&gt;
However, it is possible to implement HOM (using [[trampoline|trampolines]]), but HOM is not a language feature at this point.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== What does HOM look like? ==&lt;br /&gt;
&lt;br /&gt;
Suppose you have an [[NSArray]] containing objects you want to call a message on. Normally you would have to do something like&lt;br /&gt;
&lt;br /&gt;
  [anArray makeObjectsPerformSelector: @selector(someOtherMessage)];&lt;br /&gt;
&lt;br /&gt;
With HOM you simply write&lt;br /&gt;
&lt;br /&gt;
  [[anArray do] someOtherMessage];&lt;br /&gt;
&lt;br /&gt;
instead.&lt;br /&gt;
&lt;br /&gt;
Simply put: the ''-do'' message tells the array to call ''-someOtherMessage'' on each of its objects.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== HOM related messages ==&lt;br /&gt;
&lt;br /&gt;
To further explain how HOM works, here's a list of usual methods including a simple example.&lt;br /&gt;
&lt;br /&gt;
We start by assuming there's an array&lt;br /&gt;
&lt;br /&gt;
  NSArray *array;&lt;br /&gt;
  &lt;br /&gt;
  array = [@&amp;quot;one two three four five six seven eight nine ten&amp;quot; componentsSeparatedByString: @&amp;quot; &amp;quot;];&lt;br /&gt;
&lt;br /&gt;
Now let's see how to use HOM methods on that array and what the results will be:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* ''-select''&lt;br /&gt;
Returns members of the array if they respond YES to the argument message -- which necessarily returns a BOOL.&lt;br /&gt;
&lt;br /&gt;
  NSArray *array2 = [[array select] hasPrefix: @&amp;quot;t&amp;quot;];&lt;br /&gt;
  NSLog(@&amp;quot;%@&amp;quot;, array2);&lt;br /&gt;
&lt;br /&gt;
This will give us all numbers starting with a ''t''&lt;br /&gt;
&lt;br /&gt;
  (two, three, ten)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* ''-reject''&lt;br /&gt;
The inverse of ''-select''; it returns the elements responding NO.&lt;br /&gt;
&lt;br /&gt;
  NSArray *array3 = [[array2 reject] hasSuffix: @&amp;quot;e&amp;quot;];&lt;br /&gt;
  NSLog(@&amp;quot;%@&amp;quot;, array3);&lt;br /&gt;
&lt;br /&gt;
will return all numbers in ''array2'' that do '''not''' end with an ''e''&lt;br /&gt;
&lt;br /&gt;
  (two, ten)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* ''-collect''&lt;br /&gt;
Executes the argument message and returns an array of the responses to the argument message.&lt;br /&gt;
&lt;br /&gt;
  NSArray *array4 = [[array3 collect] stringByAppendingString: @&amp;quot; books&amp;quot;];&lt;br /&gt;
  NSLog(@&amp;quot;%@&amp;quot;, array4);&lt;br /&gt;
&lt;br /&gt;
will add ''books'' to all objects in ''array3''&lt;br /&gt;
&lt;br /&gt;
  (&amp;quot;two books&amp;quot;, &amp;quot;ten books&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* ''-do''&lt;br /&gt;
Executes the argument message.&lt;br /&gt;
&lt;br /&gt;
  NSArray *array5 = [[array collect] mutableCopy];&lt;br /&gt;
  [[array5 do] appendString: @&amp;quot; eggplants&amp;quot;];&lt;br /&gt;
  NSLog(@&amp;quot;%@&amp;quot;, array5);&lt;br /&gt;
&lt;br /&gt;
will create a mutable copy of ''array'' and append ''eggplants'' to each object.&lt;br /&gt;
&lt;br /&gt;
  (&lt;br /&gt;
    &amp;quot;one eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;two eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;three eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;four eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;five eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;six eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;seven eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;eight eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;nine eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;ten eggplants&amp;quot;&lt;br /&gt;
  )&lt;br /&gt;
&lt;br /&gt;
As you can see, ''-do'' is quite similar to ''-collect'' but works on the objects inside the collection (here ''array5'' -- the mutable copy of ''array'') instead of returning a new array (as ''-collect'' does).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* ''-each''&lt;br /&gt;
Returns each object, similar to ''-objectEnumerator''.&lt;br /&gt;
&lt;br /&gt;
  NSArray *string = [[@&amp;quot;I like to eat &amp;quot; collect]  stringByAppendingString: [array5 each]];&lt;br /&gt;
&lt;br /&gt;
This is a stupid example (because I do not like eggplants) that would give us&lt;br /&gt;
  &lt;br /&gt;
  (&lt;br /&gt;
    &amp;quot;I like to eat one eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;I like to eat two eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;I like to eat three eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;I like to eat four eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;I like to eat five eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;I like to eat six eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;I like to eat seven eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;I like to eat eight eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;I like to eat nine eggplants&amp;quot;,&lt;br /&gt;
    &amp;quot;I like to eat ten eggplants&amp;quot;&lt;br /&gt;
  )&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* ''-performAfterDelay:''&lt;br /&gt;
Performs the argument message after a specified delay.&lt;br /&gt;
&lt;br /&gt;
  [[someArray performAfterDelay: 5] delayedMessage];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* ''-ignoreExceptions''&lt;br /&gt;
Traps and ignores any exception that may occur during execution of the argument message.&lt;br /&gt;
&lt;br /&gt;
  [[someArray ignoreExceptions] messageThatMayRaiseAnException];&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
* ''-ifResponds''&lt;br /&gt;
Sends the argument message only if the receiver reponds to it.&lt;br /&gt;
Instead of writing&lt;br /&gt;
&lt;br /&gt;
  if( [receiver respondsToSelector: @selector(foobar)] ) [receiver foobar]&lt;br /&gt;
&lt;br /&gt;
you simply use&lt;br /&gt;
&lt;br /&gt;
  [[receiver ifResponds] foobar].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Objective-C]]&lt;br /&gt;
[[Category:Snippets]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>
	</entry>
	<entry>
		<id>https://mediawiki.gnustep.org/index.php?title=NSArray&amp;diff=5639</id>
		<title>NSArray</title>
		<link rel="alternate" type="text/html" href="https://mediawiki.gnustep.org/index.php?title=NSArray&amp;diff=5639"/>
		<updated>2009-05-12T08:00:49Z</updated>

		<summary type="html">&lt;p&gt;Newacct: /* From a List */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSArray.html#class$NSArray NSArray] is an immutable, integer indexed, array of objects.&lt;br /&gt;
&lt;br /&gt;
== Creating Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== From a List ===&lt;br /&gt;
&lt;br /&gt;
The most common method to create an array is to just pass it a list:&lt;br /&gt;
&lt;br /&gt;
  NSArray *array = [NSArray arrayWithObjects: @&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, [NSNull null], @&amp;quot;Jane&amp;quot;, nil];&lt;br /&gt;
&lt;br /&gt;
When creating arrays remember that any kind of object can be included, but only objects can be included. Note that &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt; values cannot be included in an array; you should use the [[NSNull|&amp;lt;code&amp;gt;[NSNull null]&amp;lt;/code&amp;gt;]] dummy object instead.&lt;br /&gt;
&lt;br /&gt;
Note that when you provide a variable-length list of objects in Objective-C, you need to &amp;quot;terminate&amp;quot; the list with &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt;. Otherwise, the function has no way of knowing how many arguments there are.&lt;br /&gt;
&lt;br /&gt;
=== Copy an Array ===&lt;br /&gt;
&lt;br /&gt;
A copy of an array can be done simply by doing the following:&lt;br /&gt;
&lt;br /&gt;
  NSArray *array1 = [NSArray arrayWithObjects: @&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, [NSNull null], @&amp;quot;Jane&amp;quot;, nil];&lt;br /&gt;
  NSArray *array2 = [NSArray arrayWithArray: array1];&lt;br /&gt;
&lt;br /&gt;
=== From an Array ===&lt;br /&gt;
&lt;br /&gt;
An array object can be made with a regular array of objects, as shown in the following example:&lt;br /&gt;
&lt;br /&gt;
  id array1[4] = {@&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, [NSNull null], @&amp;quot;Jane&amp;quot;};&lt;br /&gt;
  NSArray *array2 = [NSArray arrayWithObjects: array1 count: 4];&lt;br /&gt;
&lt;br /&gt;
Count being the number of objects from the array to be used.&lt;br /&gt;
&lt;br /&gt;
== Code chunks ==&lt;br /&gt;
&lt;br /&gt;
=== Create index dictionary ===&lt;br /&gt;
&lt;br /&gt;
Following NSArray category method creates a dictionary which works as an index by an attribute of contained objects. It uses [[Key Value Coding]].&lt;br /&gt;
&lt;br /&gt;
 - (NSDictionary *)indexDictionaryForKey:(NSString *)key&lt;br /&gt;
 {&lt;br /&gt;
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];&lt;br /&gt;
    NSDictionary        *retval;&lt;br /&gt;
    NSEnumerator        *enumerator;&lt;br /&gt;
    id                   object;&lt;br /&gt;
 &lt;br /&gt;
    enumerator = [self objectEnumerator];&lt;br /&gt;
 &lt;br /&gt;
    while( (object = [enumerator nextObject]) )&lt;br /&gt;
    {&lt;br /&gt;
        [dict setObject:object forKey:[object valueForKey:key]];&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    retval = [[NSDictionary alloc] initWithDictionary:dict];&lt;br /&gt;
    RELEASE(dict);&lt;br /&gt;
    &lt;br /&gt;
    return AUTORELEASE(retval);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
*[[NSMutableArray]]&lt;br /&gt;
*[[NSEnumerator]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Foundation]]&lt;br /&gt;
[[Category:Snippets]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>
	</entry>
	<entry>
		<id>https://mediawiki.gnustep.org/index.php?title=NSEnumerator&amp;diff=5638</id>
		<title>NSEnumerator</title>
		<link rel="alternate" type="text/html" href="https://mediawiki.gnustep.org/index.php?title=NSEnumerator&amp;diff=5638"/>
		<updated>2009-05-12T06:28:51Z</updated>

		<summary type="html">&lt;p&gt;Newacct: /* Using NSEnumerator */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSEnumerator.html#class$NSEnumerator NSEnumerator] in gnustep-base allow for iteration over collection.&lt;br /&gt;
&lt;br /&gt;
== Using NSEnumerator ==&lt;br /&gt;
&lt;br /&gt;
The following example demonstrates the use of NSEnumerator with an [[NSArray]] object:&lt;br /&gt;
&lt;br /&gt;
  NSArray *aArray = [NSArray arrayWithObjects: @&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, @&amp;quot;Jane&amp;quot;, nil];&lt;br /&gt;
  NSEnumerator *enumerator = [aArray objectEnumerator];&lt;br /&gt;
  id obj;&lt;br /&gt;
  &lt;br /&gt;
  while(obj = [enumerator nextObject])&lt;br /&gt;
    doSomethingWithObject( obj );&lt;br /&gt;
&lt;br /&gt;
== Getting an NSEnumerator from a Collection ==&lt;br /&gt;
&lt;br /&gt;
The [[NSArray]], [[NSSet]], and [[NSDictionary]] collection objects all include the -objectEnumerator method.&lt;br /&gt;
[[NSDictionary]] also includes -keyEnumerator, which enumerates over the dictionary keys instead of the objects.&lt;br /&gt;
&lt;br /&gt;
[[Category:Foundation]]&lt;br /&gt;
[[Category:Snippets]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>
	</entry>
	<entry>
		<id>https://mediawiki.gnustep.org/index.php?title=NSString&amp;diff=5637</id>
		<title>NSString</title>
		<link rel="alternate" type="text/html" href="https://mediawiki.gnustep.org/index.php?title=NSString&amp;diff=5637"/>
		<updated>2009-05-12T04:51:34Z</updated>

		<summary type="html">&lt;p&gt;Newacct: /* Substrings */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Strings in GNUstep use the [http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSString.html#class$NSString NSString] class in gnustep-base.&lt;br /&gt;
NSString is a immutable class, meaning it cannot change.&lt;br /&gt;
For mutable strings, see [[NSMutableString]].&lt;br /&gt;
See the reference manual for gnustep-base for more details about NSString.&lt;br /&gt;
&lt;br /&gt;
== Constructing Strings ==&lt;br /&gt;
&lt;br /&gt;
=== Constant Strings ===&lt;br /&gt;
&lt;br /&gt;
As you can create static instances of char* plain C, you can create strings in GNUstep that automatically flatten out to be static instances of NSString. This is done by preceding the quoted string by an &amp;quot;at&amp;quot; symbol ('@'). For example:&lt;br /&gt;
&lt;br /&gt;
  NSString * myString = @&amp;quot;This is an example of a string.&amp;quot;;&lt;br /&gt;
  NSLog(myString);&lt;br /&gt;
&lt;br /&gt;
myString is now an NSString object, and can have methods called on it like any other string.&lt;br /&gt;
&lt;br /&gt;
=== Copy Strings ===&lt;br /&gt;
&lt;br /&gt;
You can also make a copy of a string as shown in the following example:&lt;br /&gt;
&lt;br /&gt;
  NSString *myString = @&amp;quot;This is an example of a string.&amp;quot;;&lt;br /&gt;
  NSString *newString = [NSString stringWithString: myString];&lt;br /&gt;
&lt;br /&gt;
=== C Strings ===&lt;br /&gt;
&lt;br /&gt;
Or if you already have a C string you can convert it to a NSString object as follows:&lt;br /&gt;
&lt;br /&gt;
  char *myString = &amp;quot;This is a C string.&amp;quot;;&lt;br /&gt;
  ...&lt;br /&gt;
  NSString *NSString newString1 = [NSString stringWithCString: myString encoding: [NSString defaultCStringEncoding]];&lt;br /&gt;
  // Or:&lt;br /&gt;
  NSString *NSString newString2 = [NSString stringWithUTF8String: myString];&lt;br /&gt;
&lt;br /&gt;
Note that using [NSString stringWithCString] is deprecated!&lt;br /&gt;
&lt;br /&gt;
=== Formated Strings ===&lt;br /&gt;
&lt;br /&gt;
A more flexable way of creating a string, using C style format flags follows:&lt;br /&gt;
&lt;br /&gt;
  NSString *myString = [NSString stringWithFormat: @&amp;quot;This is an%@ of a string with %d integer.&amp;quot;, @&amp;quot; example&amp;quot;, 1];&lt;br /&gt;
&lt;br /&gt;
== Getting a C String ==&lt;br /&gt;
&lt;br /&gt;
You can convert an NSString object to a plain C string using the cStringUsingEncoding: method. For example:&lt;br /&gt;
&lt;br /&gt;
  NSString *newString = @&amp;quot;This is a test string.&amp;quot;;&lt;br /&gt;
  char     *theString;&lt;br /&gt;
 &lt;br /&gt;
  theString = [newString cStringWithEncoding:[NSString defaultCStringEncoding]];&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
  theString = [newString UTF8String];&lt;br /&gt;
&lt;br /&gt;
Note that using [string cString] is deprecated!&lt;br /&gt;
&lt;br /&gt;
== Reading and Writing Files With String ==&lt;br /&gt;
&lt;br /&gt;
The following example shows how you would use files with NSString:&lt;br /&gt;
&lt;br /&gt;
  // Read in the file.&lt;br /&gt;
  NSString *aString = [NSString stringWithContentsOfFile: @&amp;quot;infile.txt&amp;quot;];&lt;br /&gt;
  // Do something with our new string.&lt;br /&gt;
  aString = ''doSomething''(aString);&lt;br /&gt;
  // And finally save our changes to a file.&lt;br /&gt;
  if([aString writeToFile: @&amp;quot;outfile&amp;quot; atomically: YES])&lt;br /&gt;
    // Success code.&lt;br /&gt;
  else&lt;br /&gt;
    // Error code.&lt;br /&gt;
&lt;br /&gt;
== Substrings ==&lt;br /&gt;
&lt;br /&gt;
If you have a delimiter for breaking the string up, the easiest method to use would be the [string componentsSeparatedByString:] method.&lt;br /&gt;
The following example demonstrates this:&lt;br /&gt;
&lt;br /&gt;
  NSString *string = @&amp;quot;John, Bob, Jane&amp;quot;;&lt;br /&gt;
  NSArray *strings = [string componentsSeparatedByString: @&amp;quot;, &amp;quot;];&lt;br /&gt;
  // give us the array {@&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, @&amp;quot;Jane&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
An array of string can also be joined together with the [array componentsJoinedByString:] method as follows:&lt;br /&gt;
&lt;br /&gt;
  NSArray *strings = [NSArray arrayWithObjects: @&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, @&amp;quot;Jane&amp;quot;, nil];&lt;br /&gt;
  NSString *string = [strings componentsJoinedByString: @&amp;quot;, &amp;quot;];&lt;br /&gt;
  // creating the string @&amp;quot;John, Bob, Jane&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you need something more advanced [[NSScanner]] will be need.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
One the other hand, if you know the indexes ahead of time one of three substring methods could be used, as demonstrated in the following example:&lt;br /&gt;
&lt;br /&gt;
  NSString *string = @&amp;quot;John, Bob, Jane&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
  NSLog([string substringFromIndex: 6]);&lt;br /&gt;
  // Get the last six characters in the string (&amp;quot;, Jane&amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
  NSLog([string substringToIndex: 6]);&lt;br /&gt;
  // Get the first six characters in the string (&amp;quot;John, &amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
  NSLog([string substringWithRange: NSMakeRange(3, 6)]);&lt;br /&gt;
  // Starting at index three get a string of length six (&amp;quot;hn, Bo&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
[[http://www.gnustep.org/resources/documentation/Developer/Base/Reference/TypesAndConstants.html#type$NSRange NSRange]] is a structure used mainly in string and arrays, defining the beginning index and the offset.&lt;br /&gt;
&lt;br /&gt;
[[Category:Foundation]]&lt;br /&gt;
[[Category:Snippets]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>
	</entry>
	<entry>
		<id>https://mediawiki.gnustep.org/index.php?title=NSMutableString&amp;diff=5636</id>
		<title>NSMutableString</title>
		<link rel="alternate" type="text/html" href="https://mediawiki.gnustep.org/index.php?title=NSMutableString&amp;diff=5636"/>
		<updated>2009-05-12T04:50:54Z</updated>

		<summary type="html">&lt;p&gt;Newacct: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSString.html#class$NSMutableString NSMutableString] inherits from [[NSString]], but includes methods for changing the string.&lt;br /&gt;
&lt;br /&gt;
== Editing Strings ==&lt;br /&gt;
&lt;br /&gt;
=== Adding To Strings ===&lt;br /&gt;
&lt;br /&gt;
Strings can be added on to by appending the new substring to the end, or by inserting it any where in the middle.&lt;br /&gt;
&lt;br /&gt;
==== Appending Strings ====&lt;br /&gt;
&lt;br /&gt;
The simplest way to edit a string is by appending to the end of the string, as shown here:&lt;br /&gt;
&lt;br /&gt;
  NSMutableString *str = [NSMutableString stringWithString: @&amp;quot;short string&amp;quot;];&lt;br /&gt;
  [str appendString: @&amp;quot; made longer&amp;quot;];&lt;br /&gt;
  // str is now = @&amp;quot;short string made longer&amp;quot;&lt;br /&gt;
&lt;br /&gt;
C style formating flags can also be used as follows:&lt;br /&gt;
&lt;br /&gt;
  NSMutableString *str = [NSMutableString stringWithString: @&amp;quot;short string&amp;quot;];&lt;br /&gt;
  [str appendFormat: @&amp;quot; made longer %@.&amp;quot;, @&amp;quot;with formating&amp;quot;];&lt;br /&gt;
  // str is now = @&amp;quot;short string made longer with formating.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Inserting Substrings ====&lt;br /&gt;
&lt;br /&gt;
The more advanced method of adding to strings in by insertion, as demonstrated here:&lt;br /&gt;
&lt;br /&gt;
  NSMutableString *str = [NSMutableString stringWithString: @&amp;quot;a string&amp;quot;];&lt;br /&gt;
  [str insertString: @&amp;quot;new &amp;quot; atIndex: 2];&lt;br /&gt;
  // str is now = @&amp;quot;a new string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
=== Replacing Substrings ===&lt;br /&gt;
&lt;br /&gt;
A certain substring can be replaced with a new one as follows:&lt;br /&gt;
&lt;br /&gt;
  NSMutableString *str = [NSMutableString stringWithString: @&amp;quot;a old string&amp;quot;];&lt;br /&gt;
  [str replaceString: @&amp;quot;old&amp;quot; withString: @&amp;quot;new];&lt;br /&gt;
  // str is now = @&amp;quot;a new string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
This can also be done with ranges:&lt;br /&gt;
&lt;br /&gt;
  NSMutableString *str = [NSMutableString stringWithString: @&amp;quot;a old string&amp;quot;];&lt;br /&gt;
  // start at index 2 and include the next 3 characters&lt;br /&gt;
  [str replaceCharactersInRange: NSMakeRange(2, 3) withString: @&amp;quot;new];&lt;br /&gt;
  // str is now = @&amp;quot;a new string&amp;quot;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Foundation]]&lt;br /&gt;
[[Category:Snippets]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>
	</entry>
	<entry>
		<id>https://mediawiki.gnustep.org/index.php?title=NSString&amp;diff=5635</id>
		<title>NSString</title>
		<link rel="alternate" type="text/html" href="https://mediawiki.gnustep.org/index.php?title=NSString&amp;diff=5635"/>
		<updated>2009-05-12T04:25:32Z</updated>

		<summary type="html">&lt;p&gt;Newacct: /* Substrings */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Strings in GNUstep use the [http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSString.html#class$NSString NSString] class in gnustep-base.&lt;br /&gt;
NSString is a immutable class, meaning it cannot change.&lt;br /&gt;
For mutable strings, see [[NSMutableString]].&lt;br /&gt;
See the reference manual for gnustep-base for more details about NSString.&lt;br /&gt;
&lt;br /&gt;
== Constructing Strings ==&lt;br /&gt;
&lt;br /&gt;
=== Constant Strings ===&lt;br /&gt;
&lt;br /&gt;
As you can create static instances of char* plain C, you can create strings in GNUstep that automatically flatten out to be static instances of NSString. This is done by preceding the quoted string by an &amp;quot;at&amp;quot; symbol ('@'). For example:&lt;br /&gt;
&lt;br /&gt;
  NSString * myString = @&amp;quot;This is an example of a string.&amp;quot;;&lt;br /&gt;
  NSLog(myString);&lt;br /&gt;
&lt;br /&gt;
myString is now an NSString object, and can have methods called on it like any other string.&lt;br /&gt;
&lt;br /&gt;
=== Copy Strings ===&lt;br /&gt;
&lt;br /&gt;
You can also make a copy of a string as shown in the following example:&lt;br /&gt;
&lt;br /&gt;
  NSString *myString = @&amp;quot;This is an example of a string.&amp;quot;;&lt;br /&gt;
  NSString *newString = [NSString stringWithString: myString];&lt;br /&gt;
&lt;br /&gt;
=== C Strings ===&lt;br /&gt;
&lt;br /&gt;
Or if you already have a C string you can convert it to a NSString object as follows:&lt;br /&gt;
&lt;br /&gt;
  char *myString = &amp;quot;This is a C string.&amp;quot;;&lt;br /&gt;
  ...&lt;br /&gt;
  NSString *NSString newString1 = [NSString stringWithCString: myString encoding: [NSString defaultCStringEncoding]];&lt;br /&gt;
  // Or:&lt;br /&gt;
  NSString *NSString newString2 = [NSString stringWithUTF8String: myString];&lt;br /&gt;
&lt;br /&gt;
Note that using [NSString stringWithCString] is deprecated!&lt;br /&gt;
&lt;br /&gt;
=== Formated Strings ===&lt;br /&gt;
&lt;br /&gt;
A more flexable way of creating a string, using C style format flags follows:&lt;br /&gt;
&lt;br /&gt;
  NSString *myString = [NSString stringWithFormat: @&amp;quot;This is an%@ of a string with %d integer.&amp;quot;, @&amp;quot; example&amp;quot;, 1];&lt;br /&gt;
&lt;br /&gt;
== Getting a C String ==&lt;br /&gt;
&lt;br /&gt;
You can convert an NSString object to a plain C string using the cStringUsingEncoding: method. For example:&lt;br /&gt;
&lt;br /&gt;
  NSString *newString = @&amp;quot;This is a test string.&amp;quot;;&lt;br /&gt;
  char     *theString;&lt;br /&gt;
 &lt;br /&gt;
  theString = [newString cStringWithEncoding:[NSString defaultCStringEncoding]];&lt;br /&gt;
&lt;br /&gt;
or:&lt;br /&gt;
&lt;br /&gt;
  theString = [newString UTF8String];&lt;br /&gt;
&lt;br /&gt;
Note that using [string cString] is deprecated!&lt;br /&gt;
&lt;br /&gt;
== Reading and Writing Files With String ==&lt;br /&gt;
&lt;br /&gt;
The following example shows how you would use files with NSString:&lt;br /&gt;
&lt;br /&gt;
  // Read in the file.&lt;br /&gt;
  NSString *aString = [NSString stringWithContentsOfFile: @&amp;quot;infile.txt&amp;quot;];&lt;br /&gt;
  // Do something with our new string.&lt;br /&gt;
  aString = ''doSomething''(aString);&lt;br /&gt;
  // And finally save our changes to a file.&lt;br /&gt;
  if([aString writeToFile: @&amp;quot;outfile&amp;quot; atomically: YES])&lt;br /&gt;
    // Success code.&lt;br /&gt;
  else&lt;br /&gt;
    // Error code.&lt;br /&gt;
&lt;br /&gt;
== Substrings ==&lt;br /&gt;
&lt;br /&gt;
If you have a delimiter for breaking the string up, the easiest method to use would be the [string componentsSeparatedByString:] method.&lt;br /&gt;
The following example demonstrates this:&lt;br /&gt;
&lt;br /&gt;
  NSString *string = @&amp;quot;John, Bob, Jane&amp;quot;;&lt;br /&gt;
  NSArray *strings = [string componentsSeparatedByString: @&amp;quot;, &amp;quot;];&lt;br /&gt;
  // give us the array {@&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, @&amp;quot;Jane&amp;quot;}&lt;br /&gt;
&lt;br /&gt;
An array of string can also be joined together with the [array componentsJoinedByString:] method as follows:&lt;br /&gt;
&lt;br /&gt;
  NSArray *strings = [NSArray arrayWithObjects: @&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, @&amp;quot;Jane&amp;quot;, nil];&lt;br /&gt;
  NSString *string = [strings componentsJoinedByString: @&amp;quot;, &amp;quot;];&lt;br /&gt;
  // creating the string @&amp;quot;John, Bob, Jane&amp;quot;&lt;br /&gt;
&lt;br /&gt;
If you need something more advanced [[NSScanner]] will be need.&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
One the other hand, if you know the indexes ahead of time one of three substring methods could be used, as demonstrated in the following example:&lt;br /&gt;
&lt;br /&gt;
  NSString *string = @&amp;quot;John, Bob, Jane&amp;quot;;&lt;br /&gt;
  &lt;br /&gt;
  NSLog([string substringFromIndex: 6]);&lt;br /&gt;
  // Get the last six characters in the string (&amp;quot;, Jane&amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
  NSLog([string substringToIndex: 6]);&lt;br /&gt;
  // Get the first six characters in the string (&amp;quot;John, &amp;quot;)&lt;br /&gt;
  &lt;br /&gt;
  NSRange range = {3, 6};&lt;br /&gt;
  NSLog([string substringWithRange: range]);&lt;br /&gt;
  // Starting at index three get a string of length six (&amp;quot;hn, Bo&amp;quot;)&lt;br /&gt;
  // This can be written on one line as:&lt;br /&gt;
  // NSLog([string substringWithRange: NSMakeRange(3, 6)]);&lt;br /&gt;
&lt;br /&gt;
[[http://www.gnustep.org/resources/documentation/Developer/Base/Reference/TypesAndConstants.html#type$NSRange NSRange]] is a structure used mainly in string and arrays, defining the beginning index and the offset.&lt;br /&gt;
&lt;br /&gt;
[[Category:Foundation]]&lt;br /&gt;
[[Category:Snippets]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>
	</entry>
	<entry>
		<id>https://mediawiki.gnustep.org/index.php?title=NSArray&amp;diff=5634</id>
		<title>NSArray</title>
		<link rel="alternate" type="text/html" href="https://mediawiki.gnustep.org/index.php?title=NSArray&amp;diff=5634"/>
		<updated>2009-05-12T04:21:35Z</updated>

		<summary type="html">&lt;p&gt;Newacct: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSArray.html#class$NSArray NSArray] is an immutable, integer indexed, array of objects.&lt;br /&gt;
&lt;br /&gt;
== Creating Arrays ==&lt;br /&gt;
&lt;br /&gt;
=== From a List ===&lt;br /&gt;
&lt;br /&gt;
The most common method to create an array is to just pass it a list:&lt;br /&gt;
&lt;br /&gt;
  NSArray *array = [NSArray arrayWithObjects: @&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, [NSNull null], @&amp;quot;Jane&amp;quot;, nil];&lt;br /&gt;
&lt;br /&gt;
When creating arrays remember that any kind of object can be included, but only objects can be included. Note that &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt; values cannot be included in an array; you should use the &amp;lt;code&amp;gt;[[[NSNull]] null]&amp;lt;/code&amp;gt; dummy object instead.&lt;br /&gt;
&lt;br /&gt;
Note that when you provide a variable-length list of objects in Objective-C, you need to &amp;quot;terminate&amp;quot; the list with &amp;lt;code&amp;gt;nil&amp;lt;/code&amp;gt;. Otherwise, the function has no way of knowing how many arguments there are.&lt;br /&gt;
&lt;br /&gt;
=== Copy an Array ===&lt;br /&gt;
&lt;br /&gt;
A copy of an array can be done simply by doing the following:&lt;br /&gt;
&lt;br /&gt;
  NSArray *array1 = [NSArray arrayWithObjects: @&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, [NSNull null], @&amp;quot;Jane&amp;quot;, nil];&lt;br /&gt;
  NSArray *array2 = [NSArray arrayWithArray: array1];&lt;br /&gt;
&lt;br /&gt;
=== From an Array ===&lt;br /&gt;
&lt;br /&gt;
An array object can be made with a regular array of objects, as shown in the following example:&lt;br /&gt;
&lt;br /&gt;
  id array1[4] = {@&amp;quot;John&amp;quot;, @&amp;quot;Bob&amp;quot;, [NSNull null], @&amp;quot;Jane&amp;quot;};&lt;br /&gt;
  NSArray *array2 = [NSArray arrayWithObjects: array1 count: 4];&lt;br /&gt;
&lt;br /&gt;
Count being the number of objects from the array to be used.&lt;br /&gt;
&lt;br /&gt;
== Code chunks ==&lt;br /&gt;
&lt;br /&gt;
=== Create index dictionary ===&lt;br /&gt;
&lt;br /&gt;
Following NSArray category method creates a dictionary which works as an index by an attribute of contained objects. It uses [[Key Value Coding]].&lt;br /&gt;
&lt;br /&gt;
 - (NSDictionary *)indexDictionaryForKey:(NSString *)key&lt;br /&gt;
 {&lt;br /&gt;
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];&lt;br /&gt;
    NSDictionary        *retval;&lt;br /&gt;
    NSEnumerator        *enumerator;&lt;br /&gt;
    id                   object;&lt;br /&gt;
 &lt;br /&gt;
    enumerator = [self objectEnumerator];&lt;br /&gt;
 &lt;br /&gt;
    while( (object = [enumerator nextObject]) )&lt;br /&gt;
    {&lt;br /&gt;
        [dict setObject:object forKey:[object valueForKey:key]];&lt;br /&gt;
    }&lt;br /&gt;
 &lt;br /&gt;
    retval = [[NSDictionary alloc] initWithDictionary:dict];&lt;br /&gt;
    RELEASE(dict);&lt;br /&gt;
    &lt;br /&gt;
    return AUTORELEASE(retval);&lt;br /&gt;
 }&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
*[[NSMutableArray]]&lt;br /&gt;
*[[NSEnumerator]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Foundation]]&lt;br /&gt;
[[Category:Snippets]]&lt;/div&gt;</summary>
		<author><name>Newacct</name></author>
	</entry>
</feed>