Showing posts with label setValue. Show all posts
Showing posts with label setValue. Show all posts

Wednesday, June 19, 2013

NSMutableDictionary setValue example in Objective C (iOS).


NSMutableDictionary setValue

Adds a given key-value pair to the dictionary.

- (void)setValue:(id)value forKey:(NSString *)key

Parameters of [NSMutableDictionary setValue]
value
The value for key.
key
The key for value. Note that when using key-value coding, the key must be a string (see “Key-Value Coding Fundamentals”).

Discussion of [NSMutableDictionary setValue]
This method adds value and key to the dictionary using setObject:forKey:, unless value is nil in which case the method instead attempts to remove key using removeObjectForKey:.

NSMutableDictionary setValue example.
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
[parameters setValue:[NSNumber numberWithBool:news] forKey:@"news"];
[parameters setValue:[NSNumber numberWithBool:mails] forKey:@"mails"];

End of NSMutableDictionary setValue example article.

Wednesday, May 1, 2013

NSMutableURLRequest setValue example ios

setValue :forHTTPHeaderField:

Sets the specified HTTP header field.
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field
Parameters of [NSMutableURLRequest setValue]
value
The new value for the header field. Any existing value for the field is replaced by the new value.
field
The name of the header field to set. In keeping with the HTTP RFC, HTTP header field names are case-insensitive.
Example of [NSMutableURLRequest setValue]
NSString *myRequestString = @"sender=my%20sender&rcpt=my%20rcpt&message=hello";
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString UTF8String ] length: [ myRequestString length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString: @"http://people.bath.ac.uk/trs22/insert.php" ] ]; 
[ request setHTTPMethod: @"POST" ];
[ request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];
NSURLResponse *response;
NSError *err;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"responseData: %@", content);
Example of [NSMutableURLRequest setValue]
self.requestFor = serviceNameT;

responseData = [[NSMutableData data] retain];

SharedResponsedObject* sharedResponsedObject = [SharedResponsedObject returnSharedInstance];
LoginInfo* loginInfo = (LoginInfo*)sharedResponsedObject.loginInfo;

NSLog(@"DEVICE_TOKEN: %@, loginInfo.sessionId: %@, itemIdT: %@", DEVICE_TOKEN, loginInfo.sessionId, itemIdT);

NSString* requestStr = [NSString stringWithFormat:@"Token=%@&SessionId=%@&ItemId=%@", DEVICE_TOKEN, loginInfo.sessionId, itemIdT];


NSData *myRequestData = [ NSData dataWithBytes: [ requestStr UTF8String ] length: [ requestStr length ] ];

NSMutableURLRequest *request = [[ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:@"http://menca.com:1500/DownloadContent.aspx"]]; 

[request setHTTPMethod: @"POST" ];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[ request setHTTPBody: myRequestData ];


NSURLResponse *response;
NSError *err;
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"ServerRequestResponse::responseData: %@", content);