| Index | Submit class | Submit snippet | Submission feed | List |

NSString+FourCharCode

Language: Objective-C, Author: Belkadan Software
License: MIT/X11

Convenience methods to convert an NSString into a FourCharCode (or derived type like an OSType) and back again. Useful for Launch Services and such.

NSString+FourCharCode source preview

#import "NSString+FourCharCode.h"

@implementation NSString (ComBelkadanUtils_FourCharCode)
- (id)initWithFourCharCode:(FourCharCode)code
{
  NSData *encoded = [[NSData alloc] initWithBytes:&code length:sizeof(FourCharCode)];
  self = [self initWithData:encoded encoding:NSMacOSRomanStringEncoding];
  [encoded release];
  return self;
}

- (FourCharCode)fourCharCode
{
  if ([self lengthOfBytesUsingEncoding:NSMacOSRomanStringEncoding] == 4)
  {
    if ([self respondsToSelector:@selector(cStringUsingEncoding:)])
    {
      return *(FourCharCode *)[self cStringUsingEncoding:NSMacOSRomanStringEncoding];
    }
    else
    {
      NSData *encoded = [self dataUsingEncoding:NSMacOSRomanStringEncoding];
      return *(FourCharCode *)[encoded bytes];
    }
  }
  else
  {
    return kUnknownType;
  }
}
@end

NSString+FourCharCode header preview

#import <Foundation/NSString.h>

@interface NSString (ComBelkadanUtils_FourCharCode)
- (id)initWithFourCharCode:(FourCharCode)code;
- (FourCharCode)fourCharCode;
@end
Download Archive

Compatible with:


Comments

Name

Website

Do you hate spammers? (Answer "Yes")