2009年3月8日

Objective-C Primer

第一次探究 Objective-C,所以做了一些簡單的筆記,原本我已為跟 C 差不多,沒想到看得 "霧煞煞"。

Extension Name
.h - header files, .m - source files of C, .mm - source files of C++

與 C 不同的 keyword
==============================
NULL => nil
#include => #inport
super =>base
self => this
------------------------------------

Classes
==============================
@interface MyClass : NSObject
{
NSString * Name;
int Age;
}
- (id)initWithString: (NSString*)aName;
+ (MyClass)Create: (NSString*)aName;
@end
------------------------------------
class 的宣告必須放在 @interface 與 @end 之間,繼承的宣告方式與 C++ 相同。
method 的宣告比較特別,最前面的 "+" 代表 static method,"-" 則是一般 instance object method;method 冒號後面是參數。
------------------------------------
@implementation MyClass
- (id) initWithString : (NSString*) aName
{
if (self = [super init])
{ Name = [aName copy];
Age = 0;
return self;
}
}
@end
------------------------------------
Class 的實體,寫在 @implementation 與 @end 之間

呼叫 mathod 的方式
==============================
MyClass *obj = [[MyClass alloc] init]; // memory allocation, init is constructor
[obj initWithString: "Peter"]; // similar to obj->initWithString("Peter");
[obj release]; // release obj
------------------------------------

String
==============================
NSLog (@"Hello World!");
NSLog (@"I like Objective-C.\\n");
NSLog (@"The value is %d", nValue);
------------------------------------
Objective-C 的字串,前面固定要多加一個@,含意與 C# 不同,所以要讓字串換行,還是要再 n 之前寫兩個反斜線。

[refernece]
Objective-C Beginner's Guide: http://www.otierney.net/objective-c.html

沒有留言:

Deploying Vue & .NET with Google OAuth on GCP Cloud Run

Deploying Vue & .NET with Google OAuth on GCP Cloud Run Deploying Vue & .NET with Google OAuth on GCP Cloud Run...