ios開發(1) —— Objective-C入門教程(和java對比)

《一》、Objective-C基礎語法

一、字符串

Objective-C的字符串由雙引號包裹,並在引號前加一個@符號

title = @"Hello World";
if(title == @"hello World") {}

全局常量

1、Java代碼

使用static final 修飾符

static final AAA_BBB = 1;
2、oc
#define AAA_BBB @"Hello"

二、函數的對比

1、Java代碼
public void helloWorld(bool ishelloworld) {  
	// do something
} 
2、oc
-(void) helloWorld:(BOOL)ishelloworld{  
  // do something  
}  

三、調用類的實例方法

1、Java代碼
car.fly(1);
 // 帶多個參數
public void setColorToRedGreenBlue(float red, float green, float blue) {...}
myObj.setColorToRedGreenBlue(1.0, 0.8, 0.2);  
2、oc
// 不帶參數
[obj method];
//  帶一個參數:
[car fly: 1];
// 帶多個參數
// 非第一個參數要有類似鍵值對錶示參數`Blue:(float)blue`,**即方法名是拼接起來的**
- (void) setColorToRed: (float)red Green: (float)green Blue:(float)blue {...} //定義方法
[myColor setColorToRed: 1.0 Green: 0.8 Blue: 0.2]; //調用方法

四、函數鏈式調用

1、Java代碼
UINavigationBar bar = new UINavigationBar().autorelease();
2、oc
UINavigationBar *bar = [[[UINavigationBar alloc] init] autorelease];

五、創建對象

1、java

java中創建一個對象實例new 類構造方法()
MyObject mMyObject = new MyObject()

2、oc
  • a、創建對象需通過alloc以及init兩個消息。alloc的作用是分配內存,init則是初始化對象

    MyObject * my = [[MyObject alloc] init];

  • b、在Objective-C 2.0裏,若創建對象不需要參數,則可直接使用new
    MyObject * my = [MyObject new];

  • c、若要自己定義初始化的過程,可以重寫init方法,來添加額外的工作。(用途類似C++ 的構造函數constructor)

    - (id) init {
    	self = [super init]; // 必須調用父類的init
        if (self) {
            // do something here ...
        }
        return self;
    }
    
六、id動態類型

id 是一種通用的對象類型,它可以指向屬於任何類的對象
id obj = [Person new];

《二》、類

Objective-C的類分爲接口定義和實現兩個部分, 接口和實現以@interface@implementation開頭,都以@end結束

一、接口定義

接口定義(Interface)放在頭文件中,文件擴展名是.h

Objective-C的Interface更像是java中的Class類
Objective-C的Protocol纔像是java中的Interface接口

Objective-C的自定義一個Interface示例

  @interface MyObject {
     int memberVar1;
     id  memberVar2;
  }

  -(void) instance_method1;
  -(BOOL) instance_method2: (int) p1;
  -(UITableViewCell *) instance_method3: (int) p1 AndPar: (int) p2;
  @end
二、接口實現(implementation)

放在實現文件中,文件擴展名是.m

 @implementation MyObject {
     int memberVar3;
 }

-(void) instance_method1 {
    ....
}
-(BOOL) instance_method2: (int) p1 {
    ....
}
 -(UITableViewCell *) instance_method3: (int) p1 andPar: (int) p2 {
     ....
 }
@end

上述代碼對應的Java版

public class MyObject {
    protected int memberVar1;
    protected OtherClass memberVar2;
    private int memberVar3;

    public void instance_method1() {
        ....
    }

    public boolean instance_method2(int p1) {
        ....
    }

    public UITableViewCell instance_method3andPar(int p1, int p2) {
        ....
    }
}
三、類的靜態方法、實例方法

靜態方法:類方法有一個+加號前綴
實例方法:實例方法有一個-減號前綴

1、類定義

@interface MyObject : NSObject
    +(void) sayHello;
    -(void) sayHello2;
@end

@implementation MyObject

+(void) sayHello {
    NSLog(@"Hello, World");
}
-(void) sayHello2 {
    NSLog(@"Hello, World2");
}
@end

2、調用

[MyClass sayHello];

MyObject *mycls = [MyObject new];
[mycls sayHello2];
四、類的繼承
@interface MyObject : NSObject
@end

對應的java代碼

public class MyObject extends NSObject {
}

《三》、協議(Protocol)

相當於Java的Interface接口

1、協議的定義
@protocol Locking
    -(void)lock:(NSString)str;
@end

對應的Java代碼

publilc interface Locking {
    public void lock(String str);
}
2、協議的繼承

協議本身也可以繼承別的協議

@protocol Locking <NSObject>
    -(void)lock:(NSString)str;
@end

對應的Java代碼

public interface Locking extends NSObject {
    public void lock (String str);
}
3、可選方法

協議可以包含可選方法,顧名思義,可選方法可以不被類實現,加了@optional關鍵字,一個類在implements這個協議時,便可以不實現print:方法

@protocol Locking
@optional
    -(void)lock:(NSString)str;
@end
4、delegate協議的實現

一個類實現某些協議是寫在Interface定義裏面的, 多個協議名用逗號隔開


@interface  MyLockingController : NSObject <Locking, Drawable>

@end

對應的Java代碼

public class MyLockingController extends NSObject implements Locking, Drawable {
}

維基上的objectivec參考鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章