Sunday, May 12, 2013

NSString availableStringEncodings example ios


availableStringEncodings

Returns a zero-terminated list of the encodings string objects support in the application’s environment.
+ (const NSStringEncoding *)availableStringEncodings
Return Value
A zero-terminated list of the encodings string objects support in the application’s environment.
Discussion( NSString availableStringEncodings example )
Among the more commonly used encodings are:
See the “Constants” section for a larger list and descriptions of many supported encodings. In addition to those encodings listed here, you can also use the encodings defined for CFString in Core Foundation; you just need to call the CFStringConvertEncodingToNSStringEncoding function to convert them to a usable format.
( NSString availableStringEncodings example )
//This can print all of encoding that iPhone supports
NSMutableArray *arrayOfEncodings = [[NSMutableArray alloc] init];

const NSStringEncoding *encodings = [NSString availableStringEncodings];
while (*encodings != 0){
    [arrayOfEncodings addObject:[NSNumber numberWithUnsignedLong:*encodings]];
    encodings++;
}

for (int i=0; i<arrayOfEncodings.count; i++) {
    NSStringEncoding aNSStringEncoding = [[arrayOfEncodings objectAtIndex:i] unsignedLongValue];
    CFStringEncoding aCFStringEncoding = CFStringConvertNSStringEncodingToEncoding(aNSStringEncoding);
    CFStringRef encodingNameInCFString = CFStringGetNameOfEncoding(aCFStringEncoding);
    NSString * encodingName = (NSString*)encodingNameInCFString;
    NSLog(@"%@", encodingName);
}
( NSString availableStringEncodings example )
NSMutableArray *arrayOfEncodings = [[NSMutableArray alloc] init];

  const NSStringEncoding *encodings = [NSString availableStringEncodings];
  while (*encodings != 0){
    [arrayOfEncodings addObject:[NSNumber numberWithInt:*encodings]];
    encodings++;
  }

  NSError *readingError = nil;
  NSString yourPath = @"some path you want";
  NSData *data = [[NSData alloc] initWithContentsOfFile:yourPath    options:NSDataReadingUncached error:&readingError];

  NSStringEncoding firstEncoding = (NSStringEncoding) [((NSNumber *) [arrayOfEncodings objectAtIndex:0]) intValue];

  NSString *myString = [[NSString alloc] initWithData:data encoding:firstEncoding];