macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

changeFileAttributes:atPath:

ファイルやディレクトリ(フォルダ)の属性を変更します
-(BOOL)changeFileAttributes:(NSDictionary *)attributes:
                 atPath:(NSString *)path:

解説

ファイルやディレクトリ(フォルダ)の属性を変更します。
ファイルを変更する権限がないと変更できません。
変更できればYESを返します。
変更できなければNOを返します。
【属性を変更するときに使う辞書のキー】
● NSFileModificationDate ファイル修正日(NSDate)
● NSFilePosixPermissions POSIXのファイル属性(読み込み・書き込みなど)(NSNumber)
● NSFileExtensionHidden 拡張子を隠すか(BOOLのNSNumber)
● NSFileHFSCreatorCode クリエータタイプ(unsigned longのNSNumber)
● NSFileHFSTypeCode ファイルタイプ(unsigned longのNSNumber)
変更するときは、変更したい属性のキーの辞書だけをセットしてやればいいです。

返り値

( BOOL )

YES/NO

引数

( NSDictionary * )attributes

属性

( NSString * )path

パス

クラス

NSFileManager

Instance Methods

使用可能

10.0

参照

- fileAttributesAtPath:traverseLink:

例文

#import "MyObject.h"

@implementation MyObject

- (IBAction)myAction:(id)sender
{
//OSType *ost;
NSFileManager *myFile = [ NSFileManager defaultManager];
NSMutableDictionary *mDic =[[[NSMutableDictionary alloc] init] autorelease];

//開けるファイル拡張子の配列
    NSArray      *imgTypes    = [ NSArray arrayWithObject : @"tiff" ];
    //OpenPanelを作る
    NSOpenPanel  *opImage       = [ NSOpenPanel openPanel ];
    //OpenPanelの結果のボタン番号
    int		  opRet;  
        //OpenPanelでファイル選択   
    opRet = [ opImage runModalForDirectory : NSHomeDirectory() //どこのディレクトリを出すか
                                     file : @"Pictures" //どのファイルを選択しておくか
                                    types : imgTypes ];//選べるファイルタイプ

    if ( opRet == NSOKButton ) {  // OPENPanelのボタンがOKなら

NSLog([[myFile fileAttributesAtPath:[[ opImage filename ] stringByExpandingTildeInPath] 
                    traverseLink:YES] description]);
                    
                    
[mDic setObject:[NSDate date] forKey:@"NSFileModificationDate"];

                                                                       
[myFile changeFileAttributes:mDic atPath:[[ opImage filename ] stringByExpandingTildeInPath] ];
                
                    
NSLog([[myFile fileAttributesAtPath:[[ opImage filename ] stringByExpandingTildeInPath] 
                    traverseLink:YES] description]);


    }
}


@end