Objective-C 语言总结

通过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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值