2014/2/23

Cocoa

#23

file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html

1/9
2014/2/23

Cocoa

#23

@interface NSString ( CocoaStudy23 )
-(id) initWithData:(NSData *)data IANACharSetName:(NSString *)...
2014/2/23

Cocoa

#23

UInt8 cString[] = { 0x82, 0xA9, 0x82, 0xC8, 0x8A, 0xBF, 0x95, 0x5C, 0 };
NSData *streamedString = [...
2014/2/23

Cocoa

#23

file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html

4/9
2014/2/23

Cocoa

#23

-(void) awakeFromNib
{
NSString *targetFile = @"/Users/msyk/Documents/apple-reg.pdf";
NSString *pre...
2014/2/23

Cocoa

#23

- (NSString *)preferedMIMETypeFromFile:(NSString *)targetFile
{
FSRef ref;
FSPathMakeRef ((unsigned...
2014/2/23

Cocoa

#23

file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html

7/9
2014/2/23

Cocoa

#23

struct LSItemInfoRecord {
LSItemInfoFlags flags; //調べた項目に対するフラグ情報。ビットごとに意味があるが調べるには以下の表の定義定数を使う
OST...
2014/2/23

Cocoa

#23

file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html

9/9
Upcoming SlideShare
Loading in …5
×

Cocoa勉強会23-識別情報の変換〜文字エンコードとデータタイプ

457 views

Published on

Cocoa勉強会23
2007/10/22
識別情報の変換〜文字エンコードとデータタイプ

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
457
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Cocoa勉強会23-識別情報の変換〜文字エンコードとデータタイプ

  1. 1. 2014/2/23 Cocoa #23 file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html 1/9
  2. 2. 2014/2/23 Cocoa #23 @interface NSString ( CocoaStudy23 ) -(id) initWithData:(NSData *)data IANACharSetName:(NSString *)charSetName; @end @implementation NSString ( CocoaStudy23 ) -(id) initWithData:(NSData *)data IANACharSetName:(NSString *)charSetName { CFStringEncoding cfCharset = CFStringConvertIANACharSetNameToEncoding((CFStringRef)charSetName); NSStringEncoding nsCharset = CFStringConvertEncodingToNSStringEncoding(cfCharset); return [self initWithData:data encoding:nsCharset]; } @end @implementation AppController -(void) awakeFromNib { file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html 2/9
  3. 3. 2014/2/23 Cocoa #23 UInt8 cString[] = { 0x82, 0xA9, 0x82, 0xC8, 0x8A, 0xBF, 0x95, 0x5C, 0 }; NSData *streamedString = [NSData dataWithBytes:(void *)&cString length:sizeof cString]; NSString *testString = [[NSString alloc]initWithData:streamedString IANACharSetName:@"Shift-JIS"]; NSLog( testString ); } file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html 3/9
  4. 4. 2014/2/23 Cocoa #23 file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html 4/9
  5. 5. 2014/2/23 Cocoa #23 -(void) awakeFromNib { NSString *targetFile = @"/Users/msyk/Documents/apple-reg.pdf"; NSString *prefMIMEType = [self preferedMIMETypeFromFile:targetFile]; NSLog( prefMIMEType ); targetFile = @"file.xls"; prefMIMEType = [self preferedMIMETypeFromFile:targetFile]; NSLog( prefMIMEType ); } - (NSString *)preferedMIMETypeFromFile:(NSString *)targetFile { NSString *currentFileExtension = [targetFile pathExtension]; CFStringRef uti = UTTypeCreatePreferredIdentifierForTag( kUTTagClassFilenameExtension, (CFStringRef)currentFileExtension, NULL); CFStringRef prefferdMIMEType = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType); return (NSString *)prefferdMIMEType; } file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html 5/9
  6. 6. 2014/2/23 Cocoa #23 - (NSString *)preferedMIMETypeFromFile:(NSString *)targetFile { FSRef ref; FSPathMakeRef ((unsigned char*)[targetFile fileSystemRepresentation], &ref, NULL); CFDictionaryRef values = NULL; CFStringRef attrs[] = { kLSItemContentType, kLSItemExtension, kLSItemRoleHandlerDisplayName }; CFStringRef prefferdMIMEType = NULL; CFArrayRef attrNames = CFArrayCreate(NULL, (const void **)attrs, 3 , NULL); if ( LSCopyItemAttributes(&ref, kLSRolesAll, attrNames, &values) == noErr ) { CFTypeRef uti = CFDictionaryGetValue(values, kLSItemContentType); CFStringRef extInfo = CFDictionaryGetValue(values, kLSItemExtension); CFStringRef appInfo = CFDictionaryGetValue(values, kLSItemRoleHandlerDisplayName); NSLog ( @"%@ -> %@ -> %@", uti, extInfo, appInfo ); prefferdMIMEType = UTTypeCopyPreferredTagWithClass( uti, kUTTagClassMIMEType ); } if ( prefferdMIMEType == nil ) { /* Your Own Codes */ } return (NSString *)prefferdMIMEType; } file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html 6/9
  7. 7. 2014/2/23 Cocoa #23 file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html 7/9
  8. 8. 2014/2/23 Cocoa #23 struct LSItemInfoRecord { LSItemInfoFlags flags; //調べた項目に対するフラグ情報。ビットごとに意味があるが調べるには以下の表の定義定数を使う OSType filetype; //ファイルタイプ OSType creator; //クリエイタ CFStringRef extension; //ファイルの拡張子を示すCFStringへの参照(使用後のリリース必要) CFStringRef iconFileName; //(未使用) LSKindID kindID; //(未使用) }; typedef struct LSItemInfoRecord LSItemInfoRecord; file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html 8/9
  9. 9. 2014/2/23 Cocoa #23 file:///Users/msyk/Desktop/cocoastudy/Cocoa-Study/CocoaStudy_IDConv/CocoaStudy_IDConv.html 9/9

×