• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: bus error (newbie)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bus error (newbie)


  • Subject: Re: bus error (newbie)
  • From: Thomas Davie <email@hidden>
  • Date: Tue, 8 Feb 2005 10:04:43 +0000

Hi,

I am trying to parse a file and place it into an array of objects. The object in question "Syllable" has three string fields.

When I do this, I get a bus error.

    int count = 0;

    NSMutableArray *syllables;

    Syllable *theSyllable;

    NSString *line;

    NSString *fileContents = [NSString stringWithContentsOfFile:

        [@"~/Desktop/wgbppy.txt" stringByExpandingTildeInPath]];

    NSArray *linesOfData = [fileContents componentsSeparatedByString:@"\n"];

    NSEnumerator *arrayEnum = [linesOfData objectEnumerator];

    

    while (line = [arrayEnum nextObject]) { // for each line of data

    // get the three syllables and place it into array

    // now parse the syllables so they can be placed in Syllable fields

        NSArray *threeSyllables = [line componentsSeparatedByString:@"\t"];

// BUS ERROR OCCURS WHEN I TRY TO INIT THE OBJECT BELOW

        [theSyllable initWithPinyin: [threeSyllables[2] stringForObjectValue]

                                             bopomofo: [threeSyllables[1] stringForObjectValue]

                                            wadegiles: [threeSyllables[0] stringForObjectValue]];

        [syllables addObject: theSyllable];

<SNIP>
The problem is occurring because you are using C syntax for accessing the array, rather than passing messages to the object.  You must remember that an NSArray is an object and *not* just a pointer to a contiguous area of heap space as in C.  You need to use [threeSyllables objectAtIndex:2] rather than threeSyllables[2].

Bob
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >bus error (newbie) (From: Daniel Child <email@hidden>)

  • Prev by Date: Re: bus error (newbie)
  • Next by Date: Re: how to end editing on NSTableView?
  • Previous by thread: Re: bus error (newbie)
  • Next by thread: out of bounds exception
  • Index(es):
    • Date
    • Thread