windows 環境で NSString を試してみるよ

危ない文章をトップに晒しておく(笑)のもアレなので、objective-c の話を少し。
mac mini 上で確認してもいいのですが、エディタは QX を使いたい、ということで windows 環境で文法のテストをしています。
最終的には、xcode 上で確認をするので、GUI のところも同時に記述したいなぁと思って、次のように書いています。

まず、NSString クラスのお試しコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "base1.m"
 
- (IBAction)clickButton
{
    // 固定文字列を表示
    textField.text = @"fixed string";
 
    // 文字列の長さを取得する
    NSString *text = @"masuda tomoaki";
    int len = [text length];
    textField.text = [NSString stringWithFormat: @"length is %d", len ];
 
    {
    // 後半の部分文字列を取得する
    NSString *text = @"masuda tomoaki";
    NSString *lname = [text substringFromIndex: 7];
    textField.text = [NSString stringWithFormat: @"[%@]", lname ];
    }
    {
    // 前半の部分文字列を取得する
    NSString *text = @"masuda tomoaki";
    NSString *fname = [text substringToIndex: 6];
    textField.text = [NSString stringWithFormat: @"[%@]", fname ];
    }
    {
    // 文字列をトリムする
    NSString *text = @" masuda tomoaki ";
    NSString *name = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    textField.text = [NSString stringWithFormat: @"[%@]", name ];
    }
    {
    // 文字列を大文字にする
    NSString *text = @"MASUDA tomoaki";
    NSString *name = [text uppercaseString];
    textField.text = [NSString stringWithFormat: @"[%@]", name ];
    }
    {
    // 文字列を小文字にする
    NSString *text = @"MASUDA tomoaki";
    NSString *name = [text lowercaseString];
    textField.text = [NSString stringWithFormat: @"[%@]", name ];
    }
    {
    // 最初だけを大文字にする
    NSString *text = @"MASUDA tomoaki";
    NSString *name = [text capitalizedString];
    textField.text = [NSString stringWithFormat: @"[%@]", name ];
    }
}
 
#include "base2.m"

前後に、base1.m と base2.m がインクルードされているのミソです。この中で UI 部分をエミュレートする textField や、clickButton メソッドの実態があります。

■base1.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#import <stdio.h>
#import <Foundation/Foundation.h>
#import <Foundation/NSObject.h>
// #import <UIKit/UIKit.h>
#define IBAction void
 
@interface UITextField: NSObject
{
    @private
    NSString *_text;
}
@property (nonatomic,retain) NSString *text;
@end
@implementation UITextField
@synthesize text = _text ;
- (id)init {
    [super init];
    return self;
}
- (void)print {
    NSString *log = [NSString stringWithFormat: @"text is %@", _text];
    // NSLog(@"%@", log );
    printf("UITextField.text: %s\n", [_text cString] );
}
@end
 
@interface View : NSObject
{
    UITextField *textField;
    UITextField *textField2;
}
- (IBAction)clickButton;
@end
@implementation View
- (id)init
{
    textField = [[UITextField alloc] init];
    textField2 = [[UITextField alloc] init];
    return self;
}
- (void)print
{
    [textField print];
    [textField2 print];
}

■base2.m

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@end
 
void Main()
{
    View *view = [[View alloc] init];
    [view clickButton];
    [view print];
}
 
int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    Main();
    [pool release];
    return 0;
}

本当は、makefile を作ればいいのでしょうがソースコードレベルで書きたいので #inlucde を使っています。
テキストフィールドである UITextField クラスのダミーと、View クラスのダミーを書きます。
ボタンのクリックは、「clickButton」固定です。

base1.m と base2.m で分断しているのは、clickButton の実装が記述されているファイルが basic001.m, basic002.m のように、通番になっているからですね。ええ、原稿書きのために使っているためなのです。

これを以下のようなバッチを作ってコンパイルします。

■cc.bat

1
2
3
4
5
6
7
@echo off
set _path=%path%
set path=%path%;c:\llvm\bin;C:\GNUstep\bin;C:\GNUstep\mingw\bin;C:\GNUstep\GNUstep\System\Tools
@echo on
clang -o %~n1.exe %~n1.m -I c:/llvm/include -I c:/GNUstep/GNUstep/System/Library/Headers -L c:/GNUstep/GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString
@echo off
set path=%_path%

環境変数 path のほうは、gnuestep, clang が通るように設定します。コマンドラインでビルドをします。
一時的なので、_path に保存して戻すという…変な作り。私のところの path が長すぎてマイコンピュータの環境設定では収まりきらないので、仕方なくこうしています。整理すればいいんですけどね。

1
cc basic083.m

のようにコンパイルすることができます。

実行するとこんな感じ

■実行結果

1
2
3
4
5
D:\work\逆引きiOS\llvm>basic083
UITextField.text: fixed string
UITextField.text: change string
 
D:\work\逆引きiOS\llvm>

ええ、これを xcode project を作るスクリプトを組まないとねぇ、今月中には。

カテゴリー: Objective-C パーマリンク