Objective-C简单教程(入门)-从官方文档中挑选
来源:互联网 发布:日本福岛核事故 知乎 编辑:程序博客网 时间:2024/04/29 22:37
- 定义类,类名必须是唯一的
@interface SimpleClass : NSObject@end
- 属性定义
@interface Person : NSObject@property NSString *firstName;@property NSString *lastName;@end
@interface Person : NSObject@property (readonly) NSString *firstName;@property (readonly) NSString *lastName;@end
- 声明函数
- (void)someMethod;
- 带参数的函数
- (void)someMethodWithValue:(SomeType)value;- (void)someMethodWithFirstValue:(SomeType)value1 secondValue:(AnotherType)value2;
一般官方文档中函数表示为
someMethodWithFirstValue:secondValue:类的实现
//头文件XYZPerson.h中XYZPerson 定义@interface XYZPerson : NSObject- (void)sayHello;@endXYZPerson 类的实现@implementation XYZPerson- (void)sayHello { NSLog(@"Hello, World!");}@end
- 类方法
+ (id)string;+ (id)stringWithString:(NSString *)aString;+ (id)stringWithFormat:(NSString *)format, …;+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;+ (id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc;
- 对象方法的调用
[someObject doSomething];
- 指针同c(可以引用传递)
//带返回值的函数- (int)magicNumber { return 42;}
- 在类内部调用成员函数(self)
[self saySomething:@"Hello, world!"];
- 内存分配及初始化函数(构造函数)
+ (id)alloc;- (id)init;
- 调用
NSObject *newObject = [[NSObject alloc] init];//init函数可以自定义,即自定义构造函数
- 使用工厂方法来创建对象是非常方便的
+ (NSNumber *)numberWithBool:(BOOL)value;+ (NSNumber *)numberWithFloat:(float)value;
- 调用
NSNumber *magicNumber = [NSNumber numberWithInt:42];
- 当然也可以使用new来创建对象(和C++类似)
XYZObject *object = [XYZObject new];
- 字符串可以直接赋值
NSString *someString = @"Hello, World!”;
- Objective-C是动态语言如id类型是不确定的类型
Objective-C的空对象为nil,还有布尔类型的值为NO、YES
协议(类似java的接口或C++的抽象基类)
@protocol XYZPieChartViewDataSource- (NSUInteger)numberOfSegments;- (CGFloat)sizeOfSegmentAtIndex:(NSUInteger)segmentIndex;- (NSString *)titleForSegmentAtIndex:(NSUInteger)segmentIndex;@end
@interface XYZPieChartView : UIView@property (weak) id <XYZPieChartViewDataSource> dataSource;...@end
@interface MyClass : NSObject <MyProtocol>...@end
@interface MyClass : NSObject <MyProtocol, AnotherProtocol, YetAnotherProtocol>...@end
- 块语句(类似lamda表达式)
^{ NSLog(@"This is a block");}void (^simpleBlock)(void);simpleBlock = ^{ NSLog(@"This is a block");};void (^simpleBlock)(void) = ^{ NSLog(@"This is a block");};
0 0
- Objective-C简单教程(入门)-从官方文档中挑选
- 来源于 教程 分类 C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- C++和Objective-C混编(官方文档翻译)
- 欢迎使用CSDN-markdown编辑器
- spark on yarn-kill正在跑进程
- 【SAP BO】制作SAP BO Dashboards入门教程(一)
- 12c中保持 启动CDB后PDB的状态
- 消息的本质
- Objective-C简单教程(入门)-从官方文档中挑选
- Swift
- window phone开发链接真机测试的部署流程
- 线性回归
- You Can't Manage What You Don't Measure
- MATLAB——scatter的简单应用
- openjudge放苹果
- android 使用String.format("%.2f",67.876)自已定义语言(俄语、西班牙语)会把小数点变为逗号
- 上拉下拉刷新(2)网络加载图片