做一个默默无私奉献的程序猿

OC知识点

2017-11-01  本文已影响47人  长风留言

面向对象编程(OOP)

三大步骤

   -类

类的声明部分:

@interface 类名 : 父类名 {

// 成员变量 (数据抽象)

}

// 属性(数据抽象)

// 初始化方法

// 方法(行为抽象)

@end

类的实现部分:

@implementation 类名

// 方法的实现

@end

创建对象:

类名 *对象指针 = [[类名 alloc] 初始化方法];

发消息:

[对象指针 方法名];
返回类型 变量名 = [对象指针 方法标签:参数];
[对象指针 方法标签1:参数1 标签2:参数2];

例子:

#import <Foundation/Foundation.h>



// Step 1. 定义类
// 数据抽象: 找到和类(对象)相关的属性(找名词)
// 行为抽象: 找到和类(对象)相关的方法(找动词)

// 类的声明部分
@interface Circle : NSObject {
    // 数据抽象
    @private double _radius;
}
//方法前面如果是+ 此方法是类方法  通过类名直接调用(给类发消息)
//方法前面如果是-  此方法是对象方法 必须先创建对象才能调用(给对象发消息)


+ (instancetype) circleWithRadius:(double) radius;

// 初始化方法
- (instancetype) initWithRadius:(double) radius;

// 行为抽象

- (double) perimeter;

- (double) area;

@end

// 类的实现部分
@implementation Circle

+ (instancetype)circleWithRadius:(double)radius {
    return [[self alloc] initWithRadius:radius];
}

- (instancetype) initWithRadius:(double) radius {
    if (self = [super init]) {
        _radius = radius;
    }
    return self;
}

- (double) perimeter {
    return 2 * M_PI * _radius;
}

- (double) area {
    return M_PI * _radius * _radius;
}

@end

static const double WALL = 5.5;
static const double AISLE = 7.8;

int main() {
    // 自动释放池(离开释放池申请的内存会做一次释放操作)
    @autoreleasepool {
        printf("请输入游泳池的半径: ");
        double r;
        scanf("%lf", &r);
        // Step.2 创建对象
        // 用中缀符语法给对象发消息
        Circle *smallCircle = [[Circle alloc] initWithRadius:r];
    
        Circle *bigCircle = [[Circle alloc] initWithRadius:r + 3];
        // Step.3 给对象发消息求解问题
        printf("The price of aisle is $%.2f\n",
               ([bigCircle area] - [smallCircle area]) * AISLE);
        printf("The price of wall is $%.2f\n", [bigCircle perimeter] * WALL);
    }
    return 0;
}

系统常用类

字符串的使用(NSString)

数组(NSArray/NSMutableString)

 - containsObject:判断数组中有没有指定的对象
 - count 数组元素的个数。
 - firstObject 取数组中第一个元素
 - lastObject 取数组中最后一个元素
 - -indexOfObject: 在 数组中查找指定的对象返回位置或者NSNotFound
 - -sortedArrayUsingDescriptor:用指定的排序描述符创建一个元素有序的数组。

字典(NSDictionary/NSMutableDictionary)

类别(Category)

给已有的类打一个补丁包增强它的功能。

例如:

#import <Foundation/Foundation.h>
//对字符串进行翻转,以及加密和解密。
//建类
@interface NSString (Util)

//对字符串倒转
- (instancetype) reverse;

//进行加密
- (instancetype) encodeWithKey:(NSInteger) key;

//进行解密
- (instancetype) decodeWithKey:(NSInteger) key;

@end
// 实现方法

#import "NSString+Util.h"

@implementation NSString (Util)

- (instancetype)reverse {
  
  NSMutableString *mStr = [NSMutableString string];
  
  for (NSInteger i = self.length - 1; i >= 0; i--) {
      unichar ch = [self characterAtIndex:i];
      [mStr appendFormat:@"%C",ch ];
  }
  return [mStr copy];
}

- (instancetype)encodeWithKey:(NSInteger)key {
  
  NSMutableString * mstr = [NSMutableString string];
  for (int i = 0; i < self.length; i++) {
      unichar ch = [self characterAtIndex:i];
      ch = ch ^ key;
      [mstr appendFormat:@"%C", ch];
  }
  
  return [mstr copy];
}

- (instancetype)decodeWithKey:(NSInteger)key {
  return [self encodeWithKey:key];
}

@end

#import <Foundation/Foundation.h>
#import "NSString+Util.h"

int main(int argc, const char * argv[]) {
  @autoreleasepool {
      NSString * str = @"hello";
      NSLog(@"%@", [str reverse]);
      
      NSString *encodedstr = [str encodeWithKey:520];
      
      NSLog(@"%@", encodedstr);
      
      NSLog(@"%@", [encodedstr decodeWithKey:520]);
      
  }
  return 0;
}

提示:在给Category定义的方法命名时,最好加上命名前缀,例如命名类的前缀是QL,那个Category中的方法应加上ql_前缀,这样做主要是为了让调用者能够区分是苹果原生的方法还是Category中方法。

属性(Property)

@property (属性修饰符) 类名 变量名;

在默认的情况下,属性会自动合成成员变量以及setter(修改器)/getter(访问器)方法,如果不愿意使用自动合成,也可以在类的实现部分通过@synthesize指令指定属性如何形成。

说明:如果需要向外界暴露对象的某个属性就是用@property,这样可以死通过点语法来操作这个属性;如果不想暴露对象的某个属性(将数据保存起来)就应该直接定义成员变量。

上一篇 下一篇

猜你喜欢

热点阅读