文字列を操作する2

| コメント(0) | トラックバック(0)

「ファイルパスの操作」

「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;
}
 

トラックバック(0)

トラックバックURL: http://www.ayabin.jp/mt5/mt-tb.cgi/61

コメントする

このブログ記事について

このページは、webmasterが2010年7月31日 23:19に書いたブログ記事です。

ひとつ前のブログ記事は「文字列を操作する1」です。

次のブログ記事は「ファイルの読み書き」です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。