用C语言封装OC对象(耐心阅读,非常重要)
来源:互联网 发布:淘宝官网电话400 编辑:程序博客网 时间:2024/06/05 21:06
本文的主要内容来自这里
前言
做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("数组被销毁\n");}//打印结果void printArray(Array *arr){ for (int i = 0; i < arr->length; i++) { printf("位置:%d,姓名:%s,年龄:%d\n",i, getStringValue(getName((People*)getValueIndexAt(arr, i))),getIntegerValue(getAge((People*)getValueIndexAt(arr, i)))); }}
测试
在这里下载源码https://github.com/agelessman/MCC-2-OC-Object.git
阅读全文
0 0
- 用C语言封装OC对象(耐心阅读,非常重要)
- C语言非常重要的细节(知道一点更新一点)
- 在oc语言中对象对象属性的封装
- 黑马程序员---【OC语言】03面向对象之封装
- 用C语言实现面向对象之封装
- 用C语言封装 NSInteger,NSObject,NSString 对象
- C语言面向对象编程(一):封装与继承
- C语言面向对象编程(一):封装与继承
- C语言面向对象编程(一):封装与继承
- C语言面向对象编程(一):封装与继承
- C语言面向对象编程(一):封装与继承
- OC语言笔记(五):OC封装和复合
- Objective C(OC)面向对象三大特性之:封装
- Objective-c语言_面向对象(封装)
- Objective-C语言_面向对象(封装)
- 面向过程(C语言)和面向对象(OC语言)
- OC: 第一讲(Objective-C,面向对象的C语言)
- OC面向对象—封装
- CSS写作注意事项
- centos7下 mysql5.7 忘记密码重置
- React-navigation 官方文档中文翻译(二) Nesting Navigators
- http状态码:405 问题
- Android 混淆文件配置
- 用C语言封装OC对象(耐心阅读,非常重要)
- 详解ASP.NET 页面之间传值的几种方式
- YOLO9000: Better, Faster, Stronger
- leetcode 59. Spiral Matrix II
- Android最流行开源工具库
- 1010. 一元多项式求导 (25)
- Google/LintCode:M-搜索二维矩阵 II
- 继承
- 图论500题!