、あなたが(むしろNSString.hを自体よりも)NSPathUtilities.hで定義されている次の方法を使用のパスで動作するように:だから
@interface NSString (NSStringPathExtensions)
+ (NSString *)pathWithComponents:(NSArray *)components;
- (NSArray *)pathComponents;
- (BOOL)isAbsolutePath;
- (NSString *)lastPathComponent; // frequently-used
- (NSString *)stringByDeletingLastPathComponent; // frequently-used
- (NSString *)stringByAppendingPathComponent:(NSString *)str; // frequently-used
- (NSString *)pathExtension; // frequently-used
- (NSString *)stringByDeletingPathExtension; // frequently-used
- (NSString *)stringByAppendingPathExtension:(NSString *)str; // frequently-used
- (NSString *)stringByAbbreviatingWithTildeInPath;
- (NSString *)stringByExpandingTildeInPath;
- (NSString *)stringByStandardizingPath;
- (NSString *)stringByResolvingSymlinksInPath;
- (NSArray *)stringsByAppendingPaths:(NSArray *)paths;
- (NSUInteger)completePathIntoString:(NSString **)outputName
caseSensitive:(BOOL)flag matchesIntoArray:(NSArray **)outputArray
filterTypes:(NSArray *)filterTypes;
- (__strong const char *)fileSystemRepresentation;
- (BOOL)getFileSystemRepresentation:(char *)cname maxLength:(NSUInteger)max;
@end
を、あなたが何かをするだろうlike:
NSString *fullpath = [[path stringByAppendingPathComponent:input]
stringByAppendingPathExtension:@"txt"];
これらのメソッドは、パス区切り文字を処理します。ファイルはテキストファイルであることが知られている
場合は、NSStringの中で、次の方法を使用することができます。後者は、ファイルへの書き込みに使用することができます
- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc
error:(NSError **)error;
- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc
error:(NSError **)error;
+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc
error:(NSError **)error;
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc
error:(NSError **)error;
/* These try to determine the encoding, and return the encoding which was used.
Note that these methods might get "smarter" in subsequent releases of the
system, and use additional techniques for recognizing encodings. If nil
is returned, the optional error return indicates problem that was
encountered (for instance, file system or encoding errors). */
- (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc
error:(NSError **)error;
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc
error:(NSError **)error;
+ (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc
error:(NSError **)error;
+ (id)stringWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc
error:(NSError **)error;
/* Write to specified url or path using the specified encoding.
The optional error return is to indicate file system or encoding errors.
*/
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)useAuxiliaryFile
encoding:(NSStringEncoding)enc error:(NSError **)error;
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile
encoding:(NSStringEncoding)enc error:(NSError **)error;
。
Cocoaの多くのクラスには、ファイルやURLから読み込むメソッドがあります。たとえば、NSImage
には、ファイルから読み取るメソッドがあります。あなたは一般的にファイルハンドルよりも上位のアイテムについて考えることができます(彼らの場所はありますが)。一般的なデータの場合、常にファイルから読み込むこともできるNSData
があります。
フォルダ内の項目のリストをプログラムで取得するには、NSFileManager
を使用し、次にNSString
の方法を使用して個々の項目のパスを構築します。
ああ、もう知っていない場合は別のヒント。 Xcodeのソースコードウィンドウを開いている場合は、Commandキーを押しながら任意のクラスまたはメソッド名またはデータ型をダブルクリックして、そのオブジェクトのヘッダーファイルを開きます。たとえば、NSStringをCommand-ダブルクリックすると、NSString.hが開きます。 Command + Optionキーを押しながらダブルクリックすると、Xcodeのヘルプウィンドウが開き、強調表示された用語が検索されます。
あなたは@ ""を除いた 'input'プレーンをしたいと思うし、また** P ** ByAppendingPathComponent :(おそらく遅くなっている)文字列に余分なPがあります。私はWebブラウザで入力する必要がありません... – NSGod
@ ""なしでプレーンな入力をしたいという私の最初のコメントを気にしないでください。遅くなってきている。 – NSGod
@NSGod:これは初期のものです... – JeremyP