2011-07-31 3 views
0

J'ai cette méthode.NSDictionary. Comment créer un objet NSDictionary

[self postRequestWithURL: (NSString *) postArgs:(NSDictionary *) headerArgs: (NSDictionary *) ]; 

Comment puis-je appeler correctement cette méthode?

l'url est: http://www.google.com/reader/api/0/edit-tag?

les args de poste sont: "a=user/-/state/com.google/read&ac=edit-tags&s=feed/%@&i=%@&T=%@", entry.link, entry.identifier, tokenString

et en-tête sont:

NSString *authHeader = [NSString stringWithFormat:@"GoogleLogin %@", authString]; 
[thttpReq addValue:authHeader forHTTPHeaderField:@"Authorization"]; 

[thttpReq addValue:@"Content-Type" forHTTPHeaderField:@"application/x-www-form-urlencoded"]; 

peut sompne me aider à insérer postArgs et HeaderArgs dans un NSDictionary? Merci Paolo

Répondre

5

Essayez quelque chose comme ceci:

NSDictionary *postArgs = [NSDictionary dictionaryWithObjectsAndKeys: 
          @"user/-/state/com.google/read", @"a", 
          @"edit-tags", @"ac", 
          [NSString stringWithFormat: @"feed/%@", entry.link], @"s", 
          [NSString stringWithFormat: @"%@", entry.identifier], @"i", 
          [NSString stringWithFormat: @"%@", tokenString], @"T", 
          nil]; 

NSDictionary *headerArgs = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSString stringWithFormat: @"GoogleLogin %@", authString], @"Authorization", 
          @"application/x-www-form-urlencoded", @"Content-Type" 
          nil]; 

[self postRequestWithURL: @"http://www.google.com/reader/api/0/edit-tag" 
       postArgs: postArgs 
       headerArgs: headerArgs]; 

Notez que la liste après dictionaryWithObjectsAndKeys: contient des alternances d'objets et les clés, séparés par des virgules, suivie d'une nil finale, comme dans l'exemple ci-dessus. FWIW, les dictionnaires sont auto-libérés, vous n'avez donc pas besoin de les libérer explicitement.

2

Vous pouvez commencer par lire NSDictionary ou NSMutableDictionary documentation. Voici comment vous le faire:

NSString * feed = [NSString stringWithFormat: @"feed/%@&i=%@&T=%@", entry.link, entry.identifier, tokenString]; 
NSDictionary * parameters = [[NSDictionary alloc] initWithObjectsAndKeys: @"a", @"user/-/state/com.google/read", @"ac", @"edit-tags", @"s", feed, nil]; 
+0

merci pouvez-vous me montrer l'étape headerArgs s'il vous plaît? –

+0

voir ma réponse. Je pense que "Content-Type" est la clé et "application/x-www-form-urlencoded" est la valeur, et non l'inverse. –

Questions connexes