Quando si chiama +[NSURL URLWithString:]
ho due opzioni per costruire il mio URL:- [NSString stringByAppendingPathComponent:] o solo - [NSString stringByAppendingFormat:] per NSStrings per NSURL?
[[@"http://example.com" stringByAppendingPathComponent:@"foo"] stringByAppendingPathComponent:@"bar"]
o
[@"http://example.com" stringByAppendingFormat:@"/%@/%@",@"foo",@"bar"];
-[NSString stringByAppendingPathComponent:]
sembra la risposta più corretta, ma non perdo nulla usando -[NSString stringByAppendingFormat:]
oltre movimentazione doppie barre come nel seguente caso?
// http://example.com/foo/bar
[[@"http://example.com/" stringByAppendingPathComponent:@"/foo"] stringByAppendingPathComponent:@"bar"]
// http://example.com//foo/bar oops!
[@"http://example.com/" stringByAppendingFormat:@"/%@/%@",@"foo",@"bar"];
'stringByAppendingPathComponent' sarà, in teoria, utilizzare il "separatore del percorso di sistema" rispetto al separatore di percorso cablato nel tuo formato, rendendo il tuo codice (leggermente più) indipendente dal sistema. Ma ovviamente, Objective-C non è molto usato su Windoze, quindi non è un grosso problema. –