获取自定义类的实例数 [英] Get number of instances of custom class

查看:90
本文介绍了获取自定义类的实例数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在xcode中创建了一个自定义类:PaperPack,并定义了2个即时变量:titleauthor-

I have created a custom class in xcode: PaperPack and defined 2 instant variables: title and author-

然后我分配该类的2个实例,如下所示:

Then I alloc 2 instances of the class as below:

PaperPack *pack1 = [[PaperPack alloc] init];
pack1.title = @"Title 1";
pack1.author = @"Author";

PaperPack *pack2 = [[PaperPack alloc] init];
pack1.title = @"Title 2";
pack1.author = @"Author";

那我该如何计算并返回使用该类创建的实例数?

Then how do I count and return number of instances I have created with that class?

推荐答案

否,您无法直接获得.每当您在实例中创建时,都添加任何数组,然后使用该数组属性对其进行访问.

No you can not get directly. Whenever you create in instance add it any Array and then access it using that array properties.

例如:

NSMutableArray *allInstancesArray = [NSMutableArray new];
PaperPack *pack1 = [[PaperPack alloc] init];
pack1.title = @"Title 1";
pack1.author = @"Author";
    [allInstancesArray addObject:pack1];

PaperPack *pack2 = [[PaperPack alloc] init];
pack1.title = @"Title 2";
pack1.author = @"Author";
    [allInstancesArray addObject:pack2];

然后将其计为:

NSLog(@"TOTAL INSTANCES : %d",[allInstancesArray count]);

这篇关于获取自定义类的实例数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆