objective-c 简单入门

1、类

//类的声明

@interface myClass : 父类 {

  // {} 里声明成员变量

  @public

  @private

  @protected //默认

// 类方法

+(返回类型) class_method;

// 实例方法。 第二个参数开始需要指定参数名andPtr,第一个可以不指定。

//OC中的函数重载不是根据参数类型,而是根据参数名

//下面方法的编译后的函数名是 instance_method:andPtr:

-(返回类型) instance_method: (int) p1 andPtr: (int) p2;

@end 

//类的实现

@implementation myClass {

  // 私有成员变量在这里定义;

}

// 在这里实现interface中声明的方法

@end

2、创建对象

myClass* obj = [[myClass alloc] init];

myClass* obj = [myClass new];

3、方法调用

[ myArray insertObject:anObj atIndex:0 ];

myArray,可以是类名,也是可以是类对象,分别对应类方法,和实例方法

atIndex,第二个参数开始要与函数声明时指定参数名一致

4、属性, @property

// 使用属性声明指定成员变量的存取方式。后续单独用一篇文章说明。

@property(copy) NSString *name;  

5、字符串

NSString* myStr = @“string”;

NSString* myStr = [ NSString stringWithFormat: @"%d %s", 1 ,"quake"];

NSString* fromCString= [NSString stringWithCString:"C string" ];

6、协议 @protocal,

// 类似于java的interface

//c++的纯虚类

@protocol Locking
- (void)lock;
- (void)unlock;
@end

@interface SomeClass : SomeSuperClass <Locking>
@end

后续补充Category, property, 转发,动态类型

原文地址:https://www.cnblogs.com/jimobuwu/p/8893791.html