本文的内容主要来自于: 点击这里
最近看到一篇文章, iOS里的 NSInteger,NSObject,NSString 对象是怎么封装出来的。做过iOS开发的朋友都知道,OC是基于C封装出来的,那我们来用C实现这个吧
一、Object对象
我们来分析下如何封装一个 Object对象
如果使用C来封装对象,我们就要用到结构体
每一个Object都有一个计数器,这个计数器用来管理对象的释放
提供一定的方法,能够retain, release, 获取计数器个数
于是,有了如下实现
Object.h
#ifndef Object_h
#define 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);
#endif
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.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.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.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对象
我们进行简单分析下
数组需要一个参数length,用来获取数组中的元素个数
还需要一个参数capacity,用来说明数组的容量
AnyObject *value,数组中放着的是一组连续内存的Object对象
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.c
六、然后进行测试
下载源码https://github.com/agelessman/MCC-2-OC-Object.git