0

My program contains three textfields and I am inserting the data into an sqlite database. Now I am trying to display the data from the database in the tableview. For that I tried to add the data into an NSMutableArray and tried to print that. But it is showing a null value in the array. I don't know why I am getting a null value. cont is the NsMutableArray.

    [cont addObject:name.text];
    [cont addObject:address.text];
    [cont addObject:phone.text];

    NSLog(name.text);
    NSLog(address.text);
    NSLog(phone.text);

    int length= sizeof(cont);


    NSString *strlen = [NSString stringWithFormat:@"%d", length];
    NSLog(strlen);

    for(int i=0;i<strlen;i++)
    {
        NSArray *arr = [cont objectAtIndex:i];

        NSString *u = [NSString stringWithFormat:@"%d",i];
        NSLog(u);
        NSLog(arr);

    }

I am always getting length as 4 and null in arr. Can anyone please tell me why it is so? I know there must be some error in the code.

3
  • Don't do sizeof() to get the length of an array, use the length method. sizeof(ANY_OBJECT) will just return your platform's equivalent of sizeof(void*) Commented Aug 27, 2012 at 7:57
  • Check whether your array is allocated Commented Aug 27, 2012 at 9:32
  • @Arshad yes it is . I have included the following code : NSMutableArray * cont=[[NSMutableArray alloc]init]; Commented Aug 27, 2012 at 9:57

7 Answers 7

1

use

int length=[cont count];
       //array  count 

instead of

int length= sizeof(cont);

and refer that Link

Size of NSArray

Sample

NSMutableArray * cont=[[NSMutableArray alloc]init];

    [cont addObject:@"1"];
    [cont addObject:@"ddg"];
    [cont addObject:@"ggg"];

    for(int i=0;i<[cont count];i++)
    {
        NSLog(@"Index %d",i);
        NSLog(@" Object %@",[cont objectAtIndex:i]);
    }
    [cont release];
Sign up to request clarification or add additional context in comments.

10 Comments

yes i have used this [cont count] but i am getting length as zero.Why it is so?
@user1626972 check NSLog(@"%@",cont);
Nothing..I am getting output as ( ) .
Your array object is not added correctly .Check my edited ans
Ok i got that.But there is one problem.The values at index 0,1 and 2 are getting overwrite each time when i give the different values to the textfields.But i want the index to be increased.means next time when i submit another values, the value must be saved at the index 3 and so on.Can you please tell me how to do that?
|
1
int length= sizeof(cont);

use int length= [cont count]; instead

The sizeof(*) operator returns the size of the pointer to the NSMutableArray.

[cont count];

returns the no of elements in the array.

You are getting Null in arr because you are extracting a string [cont objectAtIndex:i] into an NSArray NSArray *arr

Use NSString *arr = [cont objectAtIndex:i];

10 Comments

yes i have used [cont count] before. But i am getting length as zero and also getting following error The array is:0 2012-08-27 14:32:16.901 db[925:f803] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' can you please tell me Why i am getting this error? Thank you for your response
at first alloc and init the NSmutableArray. Then try adding simple strings(not from the textboxes) into the array and check if you still get the error.The array remains empty after your modification. Try taking the textbox values in strings and then add to the array.
I got that.But there is one problem.The values at index 0,1 and 2 are getting overwrite each time when i give the different values to the textfields.But i want the index to be increased.means next time when i submit another values, the value must be saved at the index 3 and so on. I will post the code below :-
cont=[[NSMutableArray alloc]init]; NSString *Name = name.text; NSString *Address = address.text; NSString *Phone = phone.text; [cont addObject:Name]; [cont addObject:Address]; [cont addObject:Phone]; for(int i=0;i<[cont count];i++) { NSArray *arr = [cont objectAtIndex:i]; NSLog(@"Index %d",i); NSLog(@" Object %@",arr); } I have declared NSMutableArray *cont in @implementation . So can you please tell me how to increase the index value.
Initialize the cont array in viewDidLoad or anywhere else before the action method.everytime you enter the textbox values, the array gets initialized and adds 3 object.And change the NSArray *arr = [cont objectAtIndex:i]; line to 'NSString' *arr = [cont objectAtIndex:i];....hope this time it helps........:)
|
0
[cont addObject:phone.text];

You're adding a NSString

NSArray *arr = [cont objectAtIndex:i];

And you're trying to extract a NSArray

Also a good practice for getting the size of an array is to use its count property, not sizeof.

Comments

0

Try this, because you print an array not the value at index::

for(int i=0;i<strlen;i++)
{
    NSLog(@"%@", [cont objectAtIndex:i]);
}

Comments

0

you should use [cont count] instead of sizeof(cont)

And... did you alloc + init your NSMutableArray?

cont = [[NSMutableArray alloc] init];

and this line is wrong:

NSArray *arr = [cont objectAtIndex:i];

it should be

NSString *arr = [cont objectAtIndex:i];

1 Comment

yes i have used alloc and [cont count] before. But still i am getting length as zero and also getting following error The array is:0 2012-08-27 14:32:16.901 db[925:f803] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
0

Don't know where is your mistake but to go through un array in objective-c you should use forin loop.

for (NSString* value in cont) {
    NSString *u = [NSString stringWithFormat:@"%d",i];
    NSLog(u);
}

Comments

0
 NSString *strlen = [NSString stringWithFormat:@"%d", length];

strlen is just a NSString, but you're trying to use it as integer on line:

 for(int i=0;i<strlen;i++)
    {
       ...

    }

You should use it like this:

for(int i=0;i<[strlen intValue];i++)
        {
           ...

        }

[strlen intValue] will give you the correct result. Good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.