Objective-C 语言总结
来源:互联网 发布:php常用的正则表达式 编辑:程序博客网 时间:2024/06/05 06:26
通过Demo代码实例和伪代码,演示一般的语言使用方法,以及程序的大体结构。
更详细部分通过阅读文档了解。
//// main.m// ObjectiveCLanguage//// Created by 黄穆斌 on 16/2/3.// Copyright © 2016年 MuBinHuang. All rights reserved.///* Demo结构: main.m 主程序文件 Protocl.h 协议 ClassA.h 自定义类接口 ClassB.m 自定义类实现 ClassA+Categroy.h 自定义分类接口 ClassB+Categroy.m 自定义分类实现 */#import <Foundation/Foundation.h>#import "ClassA.h"#import "ClassA+Categroy.h"int main(int argc, const char * argv[]) { @autoreleasepool { #pragma mark 初始化与调用 ClassA * classA = [[ClassA alloc] init]; classA.propertyA = 10; [classA setPropertyA:20]; #pragma mark 控制流 NSArray * array1 = @[@"A",@"B",@"C"]; // for 循环 for (int i = 0; i < array1.count; i++) { NSLog(@"for 1 %@", array1[i]); // ... } for (NSString * str in array1) { NSLog(@"for 2 %@", str); // ... } // while 循环 int i = 0; while (i < array1.count) { NSLog(@"while %@", array1[i]); i++; } i = 0; do { NSLog(@"do %@", array1[i]); i++; } while (i < array1.count); // break And continue i = 0; while (TRUE) { if (i >= array1.count) { // 跳出循环 break; } else { i++; // 跳过后面的代码 continue; // ... // 在这里的代码永远不会执行 } } #pragma mark 选择结构 // if if (TRUE && TRUE) { // 复合条件 && 表示都,|| 表示或 // ... } else if (true) { // ... } else { // ... } // switch switch (i) { case 0: // ... break; case 1: // ... break; default: // ... break; } // Condition i = i >= array1.count ? 0 : 10; } return 0;}
//// Protocol.h// ObjectiveCLanguage//// Created by 黄穆斌 on 16/2/3.// Copyright © 2016年 MuBinHuang. All rights reserved.//#import <Foundation/Foundation.h>/* @protocol protocolName<ParentProtocolName...> @required -(void)protocolMethod; @optional -(void)optionalProtocolMethod; @end */@protocol protocolName<NSObject>@required-(void)protocolMethod;@optional-(void)optionalProtocolMethod;@end
//// ClassA.h// ObjectiveCLanguage//// Created by 黄穆斌 on 16/2/3.// Copyright © 2016年 MuBinHuang. All rights reserved.//#pragma mark - 导入文件/* #import <系统类库名称> #import "工程文件名称" */#import <Foundation/Foundation.h>#import "Protocol.h"#pragma mark - 类接口/* @interface NewClassName: ParentClassName<ProtocolName> PublicProperty; PublicMethodDeclarations; @end */@interface ClassA : NSObject<protocolName>#pragma mark PublicProperty/* @property (Attibute) Type [*] name; Attribute: 读写特性 readonly; readwrite;(Default) 多线程特性 nonatomic; atomic;(Default) 内存管理特性 weak; copy; strong;(Defalut) retain; assign; unsafe_unretained; */@property int propertyA;@property NSString * propertyB;@property (readonly) NSString * propertyC;#pragma mark PublickMethodDeclarations/* 类方法 +开头 +(BackType) methodName:(ArgumentType)argumentName secondArgumentOutName:(ArgumentType)secondArgumentName; 实例方法 -开头 -(BackType) methodName:(ArgumentType)argumentName secondArgumentOutName:(ArgumentType)secondArgumentName; */+(void) classMethod;-(void) methodA;-(int) methodB;-(NSString *) methodC;-(void) methodD:(int)argument;-(void) methodE:(int)argument secondArgument:(NSString *)secondArgument;@end
//// ClassA.m// ObjectiveCLanguage//// Created by 黄穆斌 on 16/2/3.// Copyright © 2016年 MuBinHuang. All rights reserved.//#import "ClassA.h"#pragma mark - 扩展/* @interface NewClassName() { memberDeclarations; } privateProperty; privateMethod; @end */@interface ClassA() { int _memberA; NSString * _memberB;}// 改变公有属性的读写属性@property (readwrite) NSString * propertyC;// 新增私有属性@property (atomic) NSString * propertyD;@end#pragma mark - Implementation/* @implementation NewClassName { memberDeclarations; } methodDefinitions; @end */@implementation ClassA { #pragma mark memberDeclarations int _memberC; NSString * _memberD;}#pragma mark methodDefinitions+(void) classMethod { NSLog(@"Call classMethod");}-(void) methodA { NSLog(@"Call methodA");}-(int) methodB { NSLog(@"Call methodB"); return 10;}-(NSString *) methodC { NSLog(@"Call methodC"); return @"Back";}-(void) methodD:(int)argument { NSLog(@"Call methodD:");}-(void) methodE:(int)argument secondArgument:(NSString *)secondArgument { NSLog(@"Call methodE: secondArgument:");}#pragma mark init and dealloc-(id) init { NSLog(@"Call init"); return [self initWithName:@"DefaultName"];}-(id) initWithName:(NSString *)name { NSLog(@"Call initWithName:"); //1. 调用父类初始化器 self = [super init]; //2. 确认初始化成功 if (self) { // ... } //3. 返回类实例 return self;}-(void)dealloc { //1. 自动调用:ARC 对对象属性的引用技术减持 //2. 手工实现 NSLog(@"Call dealloc"); //3. 自动调用:父类dealloc}#pragma Protocol Implementation-(void)protocolMethod { NSLog(@"Call protocolMethod");}// 可以不实现这个方法-(void)optionalProtocolMethod { NSLog(@"Call optionalProtocolMethod");}@end
//// ClassA+Categroy.h// ObjectiveCLanguage//// Created by 黄穆斌 on 16/2/3.// Copyright © 2016年 MuBinHuang. All rights reserved.///* #import "类文件" @interface ClassName(CategroyName) newMethods; @end */#import "ClassA.h"@interface ClassA(Categroy)-(void)newMethod;@end
//// ClassA+Categroy.m// ObjectiveCLanguage//// Created by 黄穆斌 on 16/2/3.// Copyright © 2016年 MuBinHuang. All rights reserved.//#import "ClassA+Categroy.h"@implementation ClassA(Categroy)-(void) newMethod { NSLog(@"Call Categroy's newMethod.");}@end
0 0
- Objective-C 语言总结
- Objective-c 语言的总结
- Objective-C 语言学习总结
- C语言和Objective-c数据类型总结
- Objective -C语言基础知识(总结篇)
- Objective C语言----类,对象,方法总结
- 黑马程序员——Objective-C语言知识整理——Objective-C语言基础知识总结
- objective-c语言基础知识总结(一)[201110081710]
- objective-c语言基础知识总结(二)[201110101650]
- iOS开发 —— Objective C语言知识点总结
- objective-c语言基础教程
- Objective-C语言知识点
- 读懂Objective-c语言
- Objective-C 语言特性
- 简介Objective-C语言
- Objective-C语言特性
- 简介Objective-C语言
- Objective-C语言:数据类型
- ubuntu下部署eclipse集成hadoop\android\web\GCC开发环境小记
- 8.11 小结
- C# List使用总结
- 【点分治】BZOJ 2599:[IOI2011]Race
- 9.2 通过Model子句进行跨行引用
- Objective-C 语言总结
- 9.2.1 示例数据
- 9.3 位置和符号引用
- 9.3.1 位置标记
- 4320: ShangHai2006 Homework|并查集
- 9.4 返回更新后的行
- 9.5 求解顺序
- 圣诞树后能找到我的记忆
- 9.6 聚合