Question

I have the following code which is giving the results which follow the code. For some reason the object added sometimes gets embedded in quotes as it should be, and sometimes it is not as in "Abtenauer" below. Can anyone tell me what might be happening?

Relevant code:

 EQBreedInfo *info = [[EQBreedInfo alloc] init];
 for (int i = 0; i < [_breedlist count]; i++)
 {
      if(!dataArray)
         {
             dataArray = [[NSMutableArray alloc] init];
         }
      info = [_breedlist objectAtIndex:i]
      [dataArray = addObject:info.breedName];

      NSLog(@"breedName - %@",info.breedName);
      NSLog(@"dataArray - %@",dataArray);
 }

Results:

2014-03-18 14:23:51.397 EquineDiary[2343:a0b] breedName - 
2014-03-18 14:23:51.397 EquineDiary[2343:a0b] dataArray - (
    ""
)
2014-03-18 14:23:56.577 EquineDiary[2343:a0b] breedName - Abaco Barb
2014-03-18 14:23:56.577 EquineDiary[2343:a0b] dataArray - (
    "",
    "Abaco Barb"
)
2014-03-18 14:24:07.333 EquineDiary[2343:a0b] breedName - Abtenauer
2014-03-18 14:24:07.334 EquineDiary[2343:a0b] dataArray - (
    "",
    "Abaco Barb",
    Abtenauer
)
2014-03-18 14:25:13.695 EquineDiary[2343:a0b] breedName - Abyssinian horse
2014-03-18 14:25:13.695 EquineDiary[2343:a0b] dataArray - (
    "",
    "Abaco Barb",
    Abtenauer,
    "Abyssinian horse"
)

Thanking you in advance

Était-ce utile?

La solution

A few things here.

  1. info = [_breedlist objectAtIndex:i] is missing a semi-colon at the end.
  2. [dataArray = addObject:info.breedName]; has an extra equal sign stuck in there.
  3. What you're looking at is the description from the dataArray variable, which adds quotation marks to the output just to make it clear to you what is actually contained in the array for debugging purposes. The quotes are not actually part of the string stored.
  4. In the for loop, use NSUInteger verses int. [_breedlist count] will return an NSUInteger and so it is best if your comparison with i is comparing two variables of the same type.

Another thing to change is the way you're allocating the initial array.

Move this section of code to before the for loop:

if (!dataArray) {
    dataArray = [[NSMutableArray alloc] init];
}

You do not need to check it on every iteration, you're just burning cycles with no benefit.

Autres conseils

A string added to an array always has quotes if there are spaces between the characters of the string or empty strings. It is a mere representation of showing a complete string while printing in the Console.

The quotes will not be present when you retrieve the object using objectAtIndex: and pass to other methods to perform necessary operations

I can tell you that first thing you should do is removing the = from here

[dataArray = addObject:info.breedName];

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top