/
/
// main.m
// FilePathUtil
//
// Created by fansunion on 15/11/29.
// Copyright (c) 2015年 demo. All rights reserved.
//#import <Foundation/Foundation.h>//演示文件路徑API
int main(int argc, const char * argv[]) {@autoreleasepool {NSString *fileName =@"path.m";NSFileManager *fm;NSString *path,*tempDir,*extention,*homeDir,*fullPath;NSArray *components;fm =[NSFileManager defaultManager];//臨時(shí)目錄tempDir = NSTemporaryDirectory();NSLog(@"The tempDir is %@",tempDir);//提取基本目錄path =[fm currentDirectoryPath];NSLog(@"The base dir is %@",[path lastPathComponent]);//fileName在當(dāng)前目錄中的完整路徑//這個(gè)地方有個(gè)問(wèn)題//本地輸出”/Users/fansunion/Library/Developer/Xcode/DerivedData/FilePathUtil-bvzjqehotbexooebruphtwcmqekz/Build/Products/Debugpath.m“//Debug和path.m之間沒(méi)有”分隔符“/",而書本中的例子是有的//最好還是手動(dòng)加上,Java中也是沒(méi)有這個(gè)分隔符,需要手動(dòng)加上的fullPath =[path stringByAppendingString:fileName];NSLog(@"The fullPath is %@",fullPath);//獲得文件擴(kuò)展名extention = [fullPath pathExtension];NSLog(@"The extentions is %@",extention);//獲得用戶的主目錄homeDir = NSHomeDirectory();NSLog(@"The home directory is %@",homeDir);//拆分路徑為各個(gè)組成部分components = [homeDir pathComponents];for(path in components){NSLog(@"%@",path);}}return 0;
}
程序輸出
2015-11-29 13:43:30.550 FilePathUtil[2861:179163] The tempDir is /var/folders/4q/5ylpds9n5n97bq_r41qvly4w0000gn/T/
2015-11-29 13:43:30.551 FilePathUtil[2861:179163] The base dir is Debug
2015-11-29 13:43:30.551 FilePathUtil[2861:179163] The fullPath is /Users/fansunion/Library/Developer/Xcode/DerivedData/FilePathUtil-bvzjqehotbexooebruphtwcmqekz/Build/Products/Debugpath.m
2015-11-29 13:43:30.551 FilePathUtil[2861:179163] The extentions is m
2015-11-29 13:43:30.552 FilePathUtil[2861:179163] The home directory is /Users/fansunion