0

When I NSLog my NSMutableArray, what I get is the following

{  ABSNFGH True,
FDGJDKG False
GFKFLDL True
PDHJHN True
FHKDMD True
DSHDMD False )

The problem is that, I am not creating this array. But I want to store each entry(e.g. ABSNFGH) in a string and its corresponding status in a bool inside a for loop . How can I do this?

Jean Paul
  • 2,389
  • 5
  • 27
  • 37
  • Can you cut and paste the actual NSLog output into the question? The structure of the data is unclear without commas and other delimiters from NSLog. – danh Mar 23 '12 at 05:48

1 Answers1

1

Suppose we name your array as yourArray

yourArray ->  {  ABSNFGH True,
                  FDGJDKG False,
                  GFKFLDL True,
                  PDHJHN True,
                  FHKDMD True,
                  DSHDMD False }

You need to add this loop:

for(int i = 0; i<[yourArray count];i++)
{
    NSArray *array = [[yourArray objectAtIndex:i] componentsSeperatedByString:@" "];
    if([array count]>1)
    { 
        NSString *stringValue = [array objectAtIndex:0];
        BOOL val = [[array objectAtIndex:1] boolValue];
    }
}

I think this should work. I have not tried implementing it personally, but may be this would work.

EDIT:

yourArray should ideally be like this:

(  
  {
      stringVal = 'ABSNFGH' 
      boolVal = True
  },
  {
      stringVal = 'FDGJDKG' 
      boolVal = False
  },
  {
      stringVal = 'GFKFLDL' 
      boolVal = True
  }
)

Refer to this link. Here you just need to replace object with a NSDictionary and you are done.

Making an array of Objects in Objective-C.

Here I have modified ennuikiller's answer from that link, to make you understand for your case:

@interface Controller

NSMutableArray *yourArray;

@end
@implementation Controller

-(void) viewDidLoad {
................
NSMutableArray *yourArray = [NSMutableArray initWithCapacity:40];

}

-(IBAction)doSomeWork
{
      NSDictionary *object = [[NSDictionary alloc] init];
      [object setValue:@"ASDFG" forKey:@"stringVal"];
      [object setBool:False forKey:@"boolVal"];

      [yourArray addObject:object];

}
@end

Hope this helps.

Community
  • 1
  • 1
Parth Bhatt
  • 19,381
  • 28
  • 133
  • 216
  • This should work if the original array contains strings, although you need to use `BOOL val = [[array objectAtIndex:1] boolValue]` – ikuramedia Mar 23 '12 at 05:46
  • 1
    @ikuragames: I have edited my answer now. I can think of only this way with this kind of array as `yourArray`. better option could have been to have `yourArray` as array of dictionaries with each dictionary containing a string and associated BOOL value. But as Jean Paul Scott said that he is not creating this array, this is the only way may be it can work out. Thanks again! – Parth Bhatt Mar 23 '12 at 05:53
  • Could you please explain how could I make `yourArray` as array of dictionaries. That would be really helpful. I hope you can add that along with your answer. – Jean Paul Mar 23 '12 at 06:15
  • 1
    @JeanPaulScott ok sure. But you need to obtain yourArray as array of dictionaries from wherever you get it. I think you are fetching this values from server. Am I right? – Parth Bhatt Mar 23 '12 at 06:18
  • @JeanPaulScott: Check my answer now. I have edited it. I have added what you wanted in the **EDIT** section of my answer – Parth Bhatt Mar 23 '12 at 06:35