Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I cannot possibly imagine how yielding the string <code>image5image4image3image2image1.jpg</code> in <code>string3</code> in your "answer" to your own question could be construed as useful or a solution (I pasted your code into an Xcode Foundation tool project w/NUMIMAGES set to 5).</p> <p><em>It seems that Objective C jumps thru hoops to make seemingly simple tasks extremely difficult.</em></p> <p>Read <a href="http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ObjectiveC/Introduction/introObjectiveC.html" rel="noreferrer">the Objective-C Guide</a> and <a href="http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/Strings/introStrings.html#//apple_ref/doc/uid/10000035i" rel="noreferrer">the String Programming Guide</a>, then make draw your conclusions. Criticizing something you don't understand is generally counter-productive (we've all done it, though -- easy to do when frustrated).</p> <p><em>I simply need to create a sequence of strings, image1.jpg, image2.jpg, etc etc</em></p> <h2>Naive solution:</h2> <pre><code>#define NUMIMAGES 5 NSMutableArray *fileNames = [NSMutableArray array]; for(int i = 0; i&lt;5; i++) [fileNames addObject: [NSString stringWithFormat: @"image%d.jpg", i]]; NSLog(@"fileNames %@", fileNames); </code></pre> <p>Outputs:</p> <pre><code>fileNames ( "image0.jpg", "image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg" ) </code></pre> <p>Note that there isn't anything particularly wrong with the naive solution. It is just more fragile, less efficient, and more code than is necessary.</p> <h2>One Possible Better Solution</h2> <ol> <li><p>Add all images into your application project as resources.</p></li> <li><p>Use the NSBundle API to grab URLs to all images at once.</p></li> </ol> <p>I.e.:</p> <pre><code>NSArray *paths = [[NSBundle mainBundle] pathsForResourcesOfType: @"jpg" inDirectory: nil]; </code></pre> <p>Not only does this remove hardcoding of image names from your code, it also allows the system to optimize directory enumeration and path generation. If you want to load only a subset of images -- say you are writing a game and have a series of images related to monsters -- then put all the related images in a single directory, add the directory to your Resources, then pass that directory's name as the second argument to the above method.</p> <p>All of this -- and much more -- is documented in the <a href="http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/LoadingResources/Introduction/Introduction.html#//apple_ref/doc/uid/10000051i" rel="noreferrer">resource programming guide</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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