用C语言封装OC对象(耐心阅读,非常重要)

本文的主要内容来自这里

前言

做iOS开发的朋友,对OC肯定非常了解,那么大家有没有想过OC中NSInteger,NSObject,NSString这些对象是怎么封装的?接下来我们就使用C语言来一部一部的实现这个封装。

Object对象

首先我们先封装一个Object对象,我们来分析一下:

  • 如果使用C来封装对象,我们就要用到结构体
  • 每一个Object都有一个计数器,这个计数器用来管理对象的释放
  • 提供一定的方法,能够retain, release, 获取计数器个数

好了,基于上边的设计呢,我们写了如下的代码:

Object.h

#include "Object.h"

// 定义Object对象
typedef struct Object {
    int retainCount;
}Object;

//宏定义方法 方便书写
#define OBJECTRETAIN(obj) objectRetain((Object*)obj)
#define OBJECTRELEASE(obj) objectRelease((Object*)obj)
#define GETRETAINCOUNT(obj) getRetainCount((Object*)obj)
void objectRetain(Object *obj);
void objectRelease(Object *obj);
int getRetainCount(Object *obj);

接下来,我们只要在.c中实现这三个方法就行了。

Object.c

#include "Object.h"
#include <stdlib.h>

void objectRetain(Object *obj) {
    obj -> retainCount += 1;
}

void objectRelease(Object *obj) {
    obj -> retainCount -= 1;
    if (obj -> retainCount <= 0) {
        free(obj);
    }
}

int getRetainCount(Object *obj) {
    return obj -> retainCount;
}

String对象

我们使用C语言的char *来封装String对象:

String.h

#ifndef String_h
#define String_h

#include <stdio.h>

typedef struct String {
    int retainCount;
    char *value;
}String;

String* newString(char* value);
char* getStringValue(String* ins);


#endif /* String_h */

String.c

#include "String.h"
#include <stdlib.h>
#include "Object.h"

String* newString(char* value) {
    String *str = malloc(sizeof(String));
    objectRetain((Object *)str);
    str -> value = value;
    return str;
}

char* getStringValue(String* ins) {
    return ins -> value;
}

Integer对象

Integer是对Int的封装。

Integer.h

#ifndef Integer_h
#define Integer_h

#include <stdio.h>

typedef struct Integer{
    int retainCount;
    int value;
    
}Integer;

Integer* newInteger(int value);
int getIntegerValue(Integer* ins);
#endif /* Integer_h */

Integer.c

#include "Integer.h"
#include <stdlib.h>
#include "Object.h"

Integer *newInteger(int value) {
    Integer *new = malloc(sizeof(Integer));
    OBJECTRETAIN(new);
    new->value = value;
    return new;
}

int getIntegerValue(Integer* ins) {
    return ins->value;
}

People对象

People对象中有两个属性,一个是String类型的姓名,一个是Integer类型的年龄,原理和上边的封装非常相似。

People.h

#ifndef People_h
#define People_h

#include <stdio.h>
#include "Integer.h"
#include "String.h"

typedef struct People{
    int retainCount;
    String* name;
    Integer* age;
    
}People;

People* newPeople(String *name,Integer *age);
String* getName(People* people);
Integer* getAge(People* people);
#endif /* People_h */

People.c

#include "People.h"
#include <stdlib.h>
#include "Object.h"

People* newPeople(String *name,Integer *age){
    People *newP = malloc(sizeof(People));
    OBJECTRETAIN(newP);
    newP->age = age;
    newP->name = name;
    return newP;
}
String* getName(People* people){
    return people->name;
}
Integer* getAge(People* people){
    return people->age;
}

Array对象

我们上边定义了好几个对象,接下来我们在定义一个数组对象,我们最终的目的是,实现类似OCNSArray的一些简单的功能,这这里我们会把People放入数组中。

  • 数组需要一个参数length,用来获取数组中的元素个数
  • 还需要一个参数capacity,用来说明数组的容量
  • AnyObject *value,数组中放着的是一组连续内存的Object对象

代码分析:

//创建数组
Array* newArray(){
    Array *arr = malloc(sizeof(Array));
    arr->length = 0;
    arr->capacity = 32;
    arr->value = allocMemoryByCapacity(arr);
    return arr;
}

//分配空间
static AnyObject* allocMemoryByCapacity(Array *arr){
    return malloc(sizeof(People*) * arr->capacity);
}

AnyObject是一个对象,他封装了Object *。malloc函数分配了一段内存后,返回了指向这段内存的指针,也就是AnyObject*.

在创建Array的时候,value就可以直接使用这个指针进行赋值,同C中的数组是一个概念。

Array.h

#ifndef Array_h
#define Array_h

#include <stdio.h>
#include "People.h"
#include "Object.h"
typedef Object* AnyObject;

typedef struct Array{
    int length;
    int capacity;
    AnyObject *value;
    
}Array;

Array* newArray();

//增加数组元素
void addElement(Array *array,AnyObject value);

//删除
Array* removeIndexAt(Array *arry,int index);

//插入
Array* insertIndexAt(Array *array,AnyObject value,int index);

//查找
AnyObject getValueIndexAt(Array *array,int index);

//获取数组长度
int getArrayLength(Array *array);

//销毁
void destroyArray(Array *array);

//打印
void printArray(Array *arr);

#endif /* Array_h */

Array.c

#include "Array.h"
#include <String.h>
#include <stdlib.h>
#include <assert.h>


//分配空间
static AnyObject* allocMemoryByCapacity(Array *arr){
    return malloc(sizeof(People*) * arr->capacity);
}

//创建数组
Array* newArray(){
    Array *arr = malloc(sizeof(Array));
    arr->length = 0;
    arr->capacity = 32;
    arr->value = allocMemoryByCapacity(arr);
    return arr;
}

//获取数组长度
int getArrayLength(Array *array){
    return array->length;
}

//增加元素
void addElement(Array *array,AnyObject value){
    if (array->length >= array->capacity) {
        array->capacity *= 2;
        AnyObject *oldValue = array->value;
        memcpy(array->value, oldValue, array->length*sizeof(AnyObject));
        free(oldValue);
    }
    OBJECTRETAIN(value);
    array->value[array->length] = value;
    array->length++;
}

//删除元素
Array* removeIndexAt(Array *arry,int index){
    assert(index >= 0 && index < arry->length);  //断言 防止越界
    
    OBJECTRELEASE(getValueIndexAt(arry, index));
    
    arry->length -- ;
    for (int i = index; i < arry->length; i++) {
        arry->value[i] = arry->value[i+1];
    }
    return arry;
}

//在指定位置增加元素
Array* insertIndexAt(Array *array,AnyObject value,int index){
    if (array->length >= array->capacity) {
        array->capacity *= 2;
        AnyObject *oldValue = array->value;
        memcpy(array->value, oldValue, array->length*sizeof(AnyObject));
        free(oldValue);
    }
    array->length++;
    
    for (int i = 1; i <= array->length - index; i++) {
        array->value[array->length - i] = array->value[array->length- i - 1];
    }
//    //将元素后移
//    for (int i = 0; i < array->length - index; i++) {
//        array->value[array->length - 1] = array->value[array->length - 1 - 1];
//    }
    //插入指定位置
    array->value[index] = value;
    OBJECTRETAIN(value);
    return array;
}



//获取某个元素
AnyObject getValueIndexAt(Array *array,int index){
    assert(index >= 0 && index < array->length);
    return array->value[index];
}

//销毁
void destroyArray(Array *array){
    free(array->value);
    free(array);
    printf("数组被销毁
");
}
//打印结果
void printArray(Array *arr){
    for (int i = 0; i < arr->length; i++) {
        printf("位置:%d,姓名:%s,年龄:%d
",i, getStringValue(getName((People*)getValueIndexAt(arr, i))),getIntegerValue(getAge((People*)getValueIndexAt(arr, i))));
    }
}

测试

在这里下载源码https://github.com/agelessman/MCC-2-OC-Object.git

原文地址:https://www.cnblogs.com/machao/p/6222401.html