「ファイルパスの操作」
「Window-Based-Application」テンプレートから制作。
結果を確認するため、「Interface Builder」のメインウィンドウに「Library」から「Label」を配置。文字列の長さによっては、「Interface Builder」で「Label」の「width」や「font size」などの属性を変更しないと、結果がすべて見えないかもしれない。
インターフェースファイル(*.h)で「UILabelクラス」のインスンス変数、プロパティ変数を宣言し、「Interface Builder」で配置した「Label」と接続する。
【サンプル】
プロジェクト名:StringMethod2
StringMethod2AppDelegete.h
#import <UIKit/UIKit.h>
@interface StringMethod2AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UILabel *myLabel;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end
StringMethod2AppDelegete.m
@synthesize window;
@synthesize myLabel;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//ファイルパスとなる文字列を作成
NSString *path=@"/usr/doc/text.txt";
//ファイルの拡張子を取得
NSString *txt=[path pathExtension];
//【結果】「txt」
//ファイル名を取得
NSString *filename=[path lastPathComponent];
//【結果】「text.txt」
//ディレクトリ名を取得
NSString *dirname=[path stringByDeletingLastPathComponent];
//【結果】「/usr/doc」
myLabel.text=dirname;
[window makeKeyAndVisible];
return YES;
}
コメントする