0

I want to make one empty array using for loop, and for this I wrote below code, but it displays array elements like 1,2,3,4,5 in NSLog statements, but I want to display empty array like ("","","","","")

Here is my code:

arrTemp=[[NSMutableArray alloc]init];   
for (int i = 0; i < 5; i++) {
    [arrTemp addObject:[NSString stringWithFormat:@"%d",i]];
}
NSLog(@"array detais is %@",arrTemp);

3 Answers 3

1

Just use the following:

[arrTemp addObject:@""];
Sign up to request clarification or add additional context in comments.

Comments

0

In swift you can do the same in one line of code:

var arrTemp = Array(count: 5, repeatedValue: "")

Comments

0

A perfectly correct answer has been provided by Alessandro. However, I think understanding why your solution did not provide the required result would be beneficial to your learning.

In the line: [arrTemp addObject:[NSString stringWithFormat:@"%d",i]];

you are adding a string with the %d format, which indicates an integer. The integer is being provided by your loop: for (int i = 0; i < 5; i++) the first item added is a 0, then a 1,..4; By using empty quotes, you have the solution you are looking for.

[arrTemp addObject:[NSString stringWithFormat:@""]];

You are not using any formatter with your string, so the statement can be simplified into the answer provided by Alessandro

-:[arrTemp addObject:@""];

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.