冗談のつもりで触ったら、とんでもなく変だったのでメモを残す事にする。
[_textview1 setText:[_textview1.text stringByAppendingString:str]];
上を見るとわかるが、textview1.textの内容に変数strの文字列を加えたものをtextview1に代入返ししている。平たくいうと textview1.textにstrの内容を追記しているわけだが、わざわざこうしないと文字列の追記はできないので要注意。
NSString *msg; NSDate *now; msg = @"ただいまの時刻: \n"; now = [NSDate date]; NSString *str = [NSString stringWithFormat:@"%@ %@\n",msg,now]; [_textview1 setText:[_textview1.text stringByAppendingString:str]]; // わざと別にしています。strにするのが自然 NSLog(@"%@ %@", msg, now);
_textform1.borderStyle = UITextBorderStyleLine;
// // 指定した日の曜日を求めてみよう。 // (NSDateでは任意の日付は調べられない。派生のNSDateFormatterを使う) NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; // 初期化 [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"]; [formatter setLocale:[NSLocale systemLocale]]; [formatter setTimeZone:[NSTimeZone systemTimeZone]]; NSDate *theDay = [formatter dateFromString:@"2001/11/14 10:22:33"]; [_textview1 setText:[_textview1.text stringByAppendingString:[NSString stringWithFormat:@"その日は %@\n",theDay]]]; NSInteger num = [_textview1.text length]; NSLog(@"その文字数は%d文字\n", num);
いやぁ、Objective-cって変わってるなぁ。
#import "testappViewController.h" @interface testappViewController () @end @implementation testappViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // // 配列を処理してみよう。 // NSArray *devices = @[@"EeePad", @"Xperia", @"Arrows", @"F-12c", @"iPhone", @"iPod touch"]; NSLog(@"devices= %@", devices); // // 数字を直接セッティングするにはこうする。 // NSArray *nums = @[@16,@2,@3,@9,@6]; NSLog(@"devices= %@", nums); // // 変数の中の数字を直接セッティングするにはカッコを使えだと。マジかよ。 // int a = 12; float b = 0.22; double c = 45.6; NSArray *xnums = @[@(a),@(b),@(c)]; NSLog(@"xlist= %@", xnums); // // 配列の中から要素をひとつ取り出すぞ。(古くて難しい書き方もあるが、そういうのはレガシーシンタックスというらしい。ほほう) // NSLog(@"僕の端末は、%@です。",devices[3]); NSLog(@"ここには、%d個の端末があります。", devices.count); // // 高速列挙とかいうやつのテスト。ゆっくりもやってみよう? // NSInteger sum = 0; // 値を取り出す for(NSString *num in nums){ sum += [num intValue]; } NSLog(@"合計 %d", sum); // // こちらは通常の配列列挙。 // sum = 0; for(int i=0;i<nums.count;i++){ sum += [nums[i] intValue]; // numsは数字の配列だが、それでも直接はダメらしい。定数だからか? } NSLog(@"合計2 %d", sum); // // 直接でない数字のテストもしてみよう。 // sum = 0; a = 10; int x = 11; int y = 12; int z = 13; NSArray *ss = @[@(a),@(x),@(y),@(z)]; for(int i=0;i<ss.count;i++){ sum += [ss[i] integerValue]; // ああ。結局直接はダメなのか。勉強になるな。 } NSLog(@"合計3 %d", sum); // // 連想配列はとりあえずうっちゃって、構造体をやろう。 // struct _box{ uint width; uint height; char type; }; typedef struct _box Box; // 上の記述はもちろん以下で置き換えられる。 /* struct _box{ uint width; uint height; char type; } Box; */ Box box1, box2; box1.width = 20; box1.height = 40; box1.type = 'a'; box2.width = 88; box2.height = 90; box2.type = 'b'; NSLog(@"Data: %d %d %c",box1.width,box1.height,box1.type); NSLog(@"Data: %d %d %c",box2.width,box2.height,box2.type); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end