首页 > objective c如何new一个对象?

objective c如何new一个对象?

这里我是指不使用框架,纯粹的objective-c语法是如何新建一个对象的?

我在网上查了许多资料,都是用NSObject的alloc后再init的方法创建一个对象
难道objective c这个语言要新建对象还必须依赖某个框架或者某个父类的存在?这不合逻辑啊


obj-c创建对象有两种方法


最近正在学习iPhone开发,书上说了嘛,objective-c是把实例化类的步骤分开来的,
alloc用来分配内存空间,然后再用init进行初始化或者用其他的方法,例如:

myArray = [[NSArray alloc] initWithObjects:@"张二",@"张三", nil];

使用class_createInstance
官方文档里内容:
Creates an instance of a class, allocating memory for the class in the default malloc memory zone.

id class_createInstance(Class cls, size_t extraBytes)
Parameters
cls
The class that you wish to allocate an instance of.
extraBytes
An integer indicating the number of extra bytes to allocate. The additional bytes can be used to store additional instance variables beyond those defined in the class definition.
Return Value
An instance of the class cls.

Declared In
runtime.h

参考资料
http://developer.apple.com/library/ma...

【热门文章】
【热门文章】