StringByAppendingString in for-loop

I want to make my line: @ "test" on @ "----"

In the console, I get: "the object returned an empty description"

I try to make for each character in foo a "-" in foo2, so I get: "----", but it does not work. Ive looked and found out to start my line first, but am I doing tho with: @ "" or am I wrong? but it is also off [NSString new].

where is the mistake?

- (void)viewDidLoad
{ 
    [super viewDidLoad];
    NSString *foo = @"test";
    NSString *foo2 = @"";

    for(int i=0; i < foo.count; i++)
    {
        [foo2 stringByAppendingString:@"-"];
    }

    NSLog(foo2);
}
+3
source share
2 answers

Edit

for(int i=0; i < foo.count; i++)
{
    [foo2 stringByAppendingString:@"-"];
}

to

for(int i=0; i < foo.length; i++)
{
    foo2 = [foo2 stringByAppendingString:@"-"];
}
+5
source

Always use NSLog(@"%@", object). Otherwise, you will get a compiler error -> "Potentially unsafe."

To handle strings, consider NSMutableString.

The same code can be written using NSMutableString as,

NSMutableString *foo2 = [[NSMutableString alloc] initWithFormat:@""];
for(int i=0; i < [foo length] ; i++){
    [foo2 appendString:@"-"];
 } 
NSLog(@"%@", foo2);
+1
source

All Articles