續前面的 Objective-C的第一個程式 HelloWorld,這裡 Mike進一步介紹如何實作 分行文字輸出。
首先是範例2-2,增加新的一行 NSLog以顯示更多的内容:
01 #import <Foundation/Foundation.h>
02
03 int main ( int argc , const char * argv [])
04 {
05 NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc ] init ];
06
07 NSLog ( @"Programming is fun!" );
08 NSLog ( @"Programming in Objective-C is even more fun!" );
09
10 [ pool drain ];
11 return 0 ;
12 }
注意:新手比較容易遇到一個問題就是每個Objective-C程式語句必須使用分号結束。
輸出的結果如下:
Programming is fun!
Programming in Objective-C is even more fun!
接下來是範例2-3 在Objective-C中使用 換行符(反斜杠\和字母n)來實作多行文字輸出
01 #import <Foundation/Foundation.h>
02
03 int main (int argc, const char * argv[])
04 {
05 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
06
07 NSLog(@"Testing...\n..1\n...2\n....3");
08 [pool drain];
09 return 0;
10 }
輸出的結果如下:
Testing...
..1
...2
....3
最後分享下筆者在CSDN上傳的源代碼連結:Programming_Is_Fun (範例2-2)
Testing (範例2-3)