原文:http://rypress.com/tutorials/objective-c/data-types/index
OC中数据类型
Primitives | NSNumber | NSDecimalNumber | NSString |
NSSet | NSArray | NSDictionary | Dates |
OC集成了所有的C的基本类型,并且定义了一些自己专属的数据类型。但是应用同样也需要一些高级的工具诸如strings,字典和日期。框架为我们定义了一些类来提供标准的面向对象的数据类型。
创建对象
在OC中有两种方式创建对象。第一,可以使用标准的alloc/init方式去创建。例如你可以按照如下示例创建NSNumber对象:
NSNumber *twentySeven = [[NSNumber alloc] initWithInt:27];
但是大多数的框架提供了对应的工厂方法,像这样:
NSNumber *twentySeven = [NSNumber numberWithInt:27];
这个创建方法为我们创建NSNumber对象并autoreleases它,在ARC出现之前非常方便,但是现在,上述两者原理上已经没有什么特殊区别了。
对象比较
Comparing objects is one of the biggest pitfalls for Objective-C beginners. There are two distinct types of equality comparisons in Objective-C:
- Pointer comparison uses the
==
operator to see if two pointers refer to the same memory address (i.e., the same object). It’s not possible for different objects to compare equal with this kind of comparison. - Value comparison uses methods like
isEqualToNumber:
to see if two objects represent the same value. It is possible for different objects to compare equal with this kind of comparison.
时间: 2024-10-21 02:38:31