用C语言封装 NSInteger,NSObject,NSString 对象

本文介绍了如何使用C语言封装Objective-C中的NSInteger, NSObject和NSString对象。通过创建结构体,实现了retainCount的管理,提供了retain, release和获取计数的方法。此外,还展示了如何封装String, Integer, People和Array对象,并提供了相应的操作方法。" 118813390,7431842,安卓逆向分析:dy急速版设备抓包与激活,"['安卓开发', '安全分析', '网络通信', '数据解析']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文的内容主要来自于: 点击这里

最近看到一篇文章, 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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值