fileExistsAtPath:
ファイルが実際に存在しているか調べます
-(BOOL)fileExistsAtPath:(NSString *)path:
解説
ファイルが実際に存在しているか調べます。
指定したパスにファイルが存在すればYESを返します。なければNOを返します。
指定したパスがシンボリックリンク(ファイルエイリアス)なら、リンクを辿って実際にファイルがあるか返します。
返り値
( BOOL )
YES/NO
引数
( NSString * )path
パス
フレームワーク
Foundation
クラス
NSFileManager
Instance Methods
使用可能
10.0
参照
- fileExistsAtPath:isDirectory:
例文
#import "MyObject.h" @implementation MyObject - (IBAction)myAction:(id)sender { NSString *str = @"~/"; NSFileManager *myFile = [ NSFileManager defaultManager]; //ファイル属性 NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys: [NSDate date],NSFileModificationDate, @"owner",@"NSFileOwnerAccountName", @"group",@"NSFileGroupOwnerAccountName", nil,@"NSFilePosixPermissions", [NSNumber numberWithBool:YES],@"NSFileExtensionHidden", nil]; //書き込むデータ NSString *str2 = @"Mutable data"; NSMutableData *dat1 = [[[NSMutableData alloc] autorelease] initWithCapacity:1]; [dat1 appendBytes:[str2 cString] length:[str2 cStringLength]]; [myFile changeCurrentDirectoryPath:[str stringByExpandingTildeInPath]]; //ファイル作成 [myFile createFileAtPath:@"createdNewFile" contents:dat1 attributes:dic]; if ([myFile fileExistsAtPath:@"createdNewFile"]){ NSLog(@"YES"); }else{ NSLog(@"NO"); } } @end