2012-04-13 12 views
4

Ich habe eine NSMutableString, wo ich einen anderen NSString an zwei Stellen einfügen muss. Mein NSMutableString istWie fügt man einen NSString in einen NSMutableString ein?

NSMutableString *webServiceLinkWithResponses = [[NSMutableString alloc] initWithString:@"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=&ansValue="]; 

Ich brauche einen String (One) nach einzufügen "question_num =" und eine andere Zeichenfolge (One) nach "ansValue =" so meine letzte Zeichenfolge sollte

http://lmsstaging.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=One&ansValue=One

wie sein

Irgendwelche Vorschläge dazu?

Antwort

4

Versuchen Sie folgendes:

NSMutableString *webServiceLinkWithResponses = [[NSMutableString alloc] initWithString:[NSString stringWithFormat: @"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@",yourFirstString,yourSecondString]; 

Sag mir, wenn es hilft!

+0

Vielen Dank ... Wie habe ich das verpassen ?? Trotzdem danke für die Lösung. Ich war völlig leer. –

+3

Warum erstellen Sie einen 'NSString' mit' stringWithFormat: ', der dann an 'NSMutableString's' initWithString:' übergeben wird, wenn Sie einfach 'NSMutableString's 'initWithFormat:'? – mttrb

+0

Ja, du hattest Recht. Das können wir auch auf diese Weise erreichen ... NSMutableString * webServiceLinkWithResponses = [NSMutableString-Zuweisung] initWithFormat: @ "http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype = r & question_num =% @ & ansValue =% @ ", yourFirstString, yourSecondString]; –

2

Mein Weg wäre:

NSMutableString *webServiceLinkWithResponses = [[[NSString stringWithFormat: @"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@",yourFirstString,yourSecondString] mutableCopy] autorelease]; 

Nach einem Hinweis von dreamlax können Sie auch:

NSMutableString *webServiceLinkWithResponses = [NSMutableString stringWithFormat:@"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@", stringOne, stringTwo]; 
+3

Überspringen Sie alternativ die 'mutableCopy' /' Autorelease' und verwenden Sie einfach 'NSMutableString stringWithFormat:]' – dreamlax

+0

Total richtig .. lassen Sie mich meine Antwort aktualisieren. –

5

Im Folgenden wird schaffen eine nicht retardierte NSMutableString:

NSMutableString *webServiceLinkWithResponses = [NSMutableString stringWithFormat:@"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@", stringOne, stringTwo]; 

Wenn Sie müssen es beibehalten, verwenden Sie einfach [[NSMutableString alloc] initWithFormat:@"..."]:

NSMutableString *webServiceLinkWithResponses = [[NSMutableString alloc] initWithFormat:@"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@", stringOne, stringTwo]; 

Benötigen Sie es wirklich eine veränderbare Zeichenfolge, werden Sie es ändern, sobald es erstellt wurde? Wenn nicht einfach die NSMutableString zu NSString, z.B. (Dies liefert ein Autoreleased NSString, verwenden [NSString alloc] initWithFormat:] wenn Sie es erhalten):

NSString *webServiceLinkWithResponses = [NSString stringWithFormat:@"http://lmsstaging.2xprime.com/services/ParticipantService.cfc?method=setVideoExamResults&student_id=10082&course_id=VRT_TRA&lesson=904&examtype=r&question_num=%@&ansValue=%@", stringOne, stringTwo]; 
Verwandte Themen