Saturday, May 18, 2013

NSString initWithData encoding example ios

[NSString initWithData encoding]

Returns an NSString object initialized by converting given data into Unicode characters using a given encoding.
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding
An NSData object containing bytes in encoding and the default plain text format (that is, pure content with no attributes or other markups) for that encoding.[NSString initWithData encoding]
The encoding used by data.
Return Value of [NSString initWithData encoding]
An NSString object initialized by converting the bytes in data into Unicode characters using encoding. The returned object may be different from the original receiver. Returns nil if the initialization fails for some reason (for example if datadoes not represent valid data for encoding).
[NSString initWithData encoding] example
NSString to NSData:
NSString* str= @"teststring";
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding];
NSData to NSString:
NSString* newStr = [[NSString alloc] initWithData:theData
[NSString initWithData encoding] example
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    [receivedData appendData:data];
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
    NSString * result_string=[[[NSString alloc] initWithData:receivedData encoding:NSNonLossyASCIIStringEncoding] autorelease];
[NSString initWithData encoding] example
SomeClass *someObject;
NSString *stringReply;
stringReply = [[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
[someObject methodWithString:stringReply];