Objective C - addObject to store array return extra bracket for me how to remove

梦想的初衷 提交于 2019-12-13 21:07:45

问题


my data coming in this format

(
   (
      Male,
      Female
   ),
   (
      First,
      Second,
      Third
   )
)

and I want in this format

(
   Male,
   Female
),
(
   First,
   Second,
   Third
)

how to remove extra bracket from array

if ([[allObject objectForKey:@"Fid"] isKindOfClass:[NSArray class]])
{
        NSArray *arr = (NSArray *)[allObject objectForKey:@"Fid"];
        for (int i =0; i<arr.count; i++) {
            if ([[arr valueForKey:@"Values"]objectAtIndex:i] != (id)[NSNull null] ) {
                NSMutableArray *fetchArray = [[NSMutableArray alloc]init];
                [fetchArray addObjectsFromArray:[[arr valueForKey:@"Values"] objectAtIndex:i]];
                [fetchValue addObject:fetchArray];
            }
        }
}

I have tried addObject and addObjectFromArray but in addObjectFromArray output comes in (male,female,first,second,third) and i want like (male,female),(first,second,third)


回答1:


This line [fetchValue addObject:fetchArray]; is the problem. You need use = and not addObject.

In your fetchValuemutableArray you are adding other mutableArray fetchArray, finally you get array of arrays. Only use =




回答2:


Objective-C doesn't care about you want. In this case, you can't get what you want.

You can have an array containing two arrays. That's what you got. You can't have nothing containing two arrays. It just doesn't make sense.

And note that your data isn't coming in this format. What you posted is the output of NSLog, it's not your data.



来源:https://stackoverflow.com/questions/35771327/objective-c-addobject-to-store-array-return-extra-bracket-for-me-how-to-remove

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!