سؤال

I am using Mantle to parse some JSON data from Yelp.

For each business returned I get an NSArray of categories. This would be an example:

yelpCategories =     (
            (
        "Wine Bars",
        "wine_bars"
    ),
            (
        "Ice Cream & Frozen Yogurt",
        icecream
    )
);

yelpCategories is the name of the array that I save. Later on I am trying to parse the array into a string:

    NSMutableString *yelpCats = [[NSMutableString alloc] init];
    for (NSObject * obj in business.yelpCategories)
    {
        [yelpCats appendString:[NSString stringWithFormat:@"%@,",[obj description]]];
    }

The issue is with the above. I am being returned a string just as "(" so I must be accessing the array incorrectly. How can I correctly access each object, ideally I would be looking for the end string o be @"Wine Bars, Ice Cream & Frozen Yogurt".

EDIT

The categories array: (
        (
        Pubs,
        pubs
    )
)

FINAL EDIT - Proposed Solution

for (NSArray *cats in business.yelpCategories)
{
    NSString *category = [cats objectAtIndex:0];
    if ([category length] > 0) {
        category = [category substringToIndex:[category length] - 1];
    }

    if (cats == business.yelpCategories.lastObject) {
        [yelpCats appendString:[NSString stringWithFormat:@"%@",category]];
    } else {
        [yelpCats appendString:[NSString stringWithFormat:@"%@, ",category]];
    }
}
cell.yelpCategories.text = yelpCats;
هل كانت مفيدة؟

المحلول 3

for (NSArray *cats in business.yelpCategories)
{
    NSString *category = [cats objectAtIndex:0];
    if ([category length] > 0) {
        category = [category substringToIndex:[category length] - 1];
    }

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

نصائح أخرى

Using the description of the object gives you what you see in the debugger, which includes extra carriage returns.

What you want to do is something like:

yelpCats = [yelpCategories componentsJoinedByString:@", "];

@jeffamaphone 's answer is the correct and best way of doing things however what your doing will almost work, I think your just confused on the contents of the array.

The yelpCategories array is an array of strings so you don't need to call stringWithFormat or call the description method. In fact [obj description] will return a string so you didn't even need stringWithFormat in your example and you would have gotten the same output. To make your original method work change to:

NSMutableString *yelpCats = [[NSMutableString alloc] init];
for (id obj in business.yelpCategories)
{
    //obj is a string so we can just append it.
    [yelpCats appendString:obj]];
}

Also noticed I changed NSObject *obj to just id obj, this is the idiomatic way and shorthand way of declaring NSObjects in objective-c. In this example however I would actually use (NSString *category in business.yelpCategories) instead for better readability. In this case you are declaring to everyone that you expect each object in the array to be a string and then if you wanted to use NSString methods on it inside the loop then you don't have to cast it.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top