0

I am using Mantle to parse some business JSON. At present we go through an array of JSON objects for the business categories with the following:

    NSMutableString *stringCats = [[NSMutableString alloc] init];
    for (NSArray *cats in business.yelpCategories)
    {
        NSString *category = [cats objectAtIndex:0];
        if ([category length] > 0) {
            if ([category hasSuffix:@"s"]) {
                 category = [category substringToIndex:[category length] - 1];
            }
        }

        if (cats == business.yelpCategories.lastObject) {
            [stringCats appendString:[NSString stringWithFormat:@"%@",category]];
        } else {
            [stringCats appendString:[NSString stringWithFormat:@"%@, ",category]];
        }
    }
    searchResultCell.stringCategories.text = stringCats;

This loops through the array of categories removes the last letter if an 's' then appends the string into one string.

This is currently completed in the cellForRowAt.. and I feel like this is not the correct place to do this sort of work.

What I would like to do is parse this data into a string on the business model created with Mantle originally rather than complete this for each cell.

Question
How do I create a custom NSValueTransformer based on our current work above to transform the JSON array into a string on the model instead?

StuartM
  • 6,743
  • 18
  • 84
  • 160
  • I don't believe that NSArray has an `appendString` method. (In fact, it's hard to make heads or tails out of your code.) – Hot Licks May 14 '14 at 18:13
  • @HotLicks - Added an update edit – StuartM May 14 '14 at 21:29
  • What have you tried with respect to writing an NSValueTransformer? It seems to me that all you need to do is to create an `NSValueTransformer` subclass and put your existing logic into the `-transformedValue:` method. – bdesham May 14 '14 at 21:35
  • I have not tried to write the `NSValueTransformer` as I am not 100% on how they work. In this case I need a string from an Array of Arrays is that possible with a subclass of NSValueTransformer, could you explain more please – StuartM May 14 '14 at 22:21
  • Can you post an example of your JSON, and clarify the desired end result. As I understand it, you are creating an array of models, and on each model you want a string which is a concatenation of an array of strings? – David Snabel-Caunt May 15 '14 at 11:20

0 Answers0