Question

As I looked for answers from google and here most of the answer says that you are trying to use length on an NSArray which is not supported.

The problem here is I don't even use any NSArray or length in my code.

I got a

NSMutableArray *filteredContent;   

where filteredContent will contain dictionaries from a plist.

Everything runs great until writing cell.textLabel.text on the cell for tableView.
Checked with NSLog and the content is indeed an array.

This is how I try to write the cell text:

cell.textLabel.text=[[self.filteredContent objectAtIndex:indexPath.row] valueForKey:@"recipeName"];

But it gives me error so i changed it to:

NSString *myValue = [self.filteredContent valueForKey:@"recipeName"];
cell.textLabel.text=myValue;

Yet the result is the same. I have no idea what I am getting this error.

Further details:

[results addObject:@[recipe]];

This is where i create my main array and than pass it to the PageView with segue filteredContent = results


EDIT:

recipe = arrayOfPlist[i]; where arrayOfPlist

NSArray *arrayOfPlist = [[NSArray alloc] initWithContentsOfFile:path];
//Output to NSLog(@"FIltered Content :  %@", self.filteredContent);
FIltered Content :  (
        (
            {
                category = main;
                difficultyLevel = "3/5";
                numberOfPerson = 5;
                recipeDetail = "Bulguru koy su koy beklet pisir ye ";
                recipeImage = "nohutlu-pilav.jpg";
                recipeIngredients = "pirinc,tereyag tuz,bulgur";
                recipeName = "Bulgurlu Pilav";
                time = "25 dk";
            }
        ),
        (
            {
                category = main;
                difficultyLevel = "3/5";
                numberOfPerson = 5;
                recipeDetail = "Bulguru koy su koy beklet pisir ye ";
                recipeImage = "nohutlu-pilav.jpg";
                recipeIngredients = "pirinc,tereyag tuz,bulgur";
                recipeName = "Bulgurlu Pilav";
                time = "25 dk";
            }
        )
    )

    2014-04-27 02:47:41.704 deneme2[19820:60b] VALUE IN FILTERED TABLE  is (
        "Bulgurlu Pilav" // this is what i want to write to the cell label and i get it with myValue-look a bit above
    )
Was it helpful?

Solution

Based on the output of your data, you have an extra array in there. So you want this:

cell.textLabel.text = self.filteredContent[indexPath.row][0][@"recipeName"];

Each element of your filteredContentArray is another array. Each of those inner arrays has the dictionary with the data you want.

OTHER TIPS

Do what @rmaddy suggested or:

Change:

[results addObject:@[recipe]];

to:

[results addObject:recipe]

Explanation:

NSInvalidArgumentException', reason: '-[__NSArrayI length : unrecognized selector sent to instance

This error tells us:

  1. Object is an NSArray (somewhere, somehow)
  2. Method called on this NSArray object was length
    • But NSArray does not have a length method
    • It seems somewhere a call to length method was sent, most probably on an NSString object that is actually holding an NSArray object rather than an NSString object.

Firstly note that when you specify:

  1. @[]

So when you do: [results addObject:@[recipe]];:

  • @[recipe]
    is equivalent to
  • [NSArray alloc] initWithObjects:recipe, nil]

So... you're basically taking recipe, putting it in an array, then adding this array object within results (which is another array).

Later when you come to PageView

[[self.filteredContent objectAtIndex:indexPath.row] valueForKey:@"recipeName"];

will return you an array of values for the key recipeName.

So basically, your string is holding an array and not the string object.
Now... whether you called length on it or not, somewhere in the lifecycle, length is being called on cell.textLabel.text and raising this error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top