跳到主要内容

OC常见类型

基础类型扩展

除了 C 语言的基本数据类型(如 int、float、double、char 等),Objective-C 还提供了一些新的基本类型和对象类型。

  • NSNumber
  • NSInteger
  • CGFloat
危险

CGFloat 和 NSInteger 都不是对象,它们是基础数据类型的别名,设计用于在不同平台上提供一致的行为和性能。

对象类型

  1. NSObject

NSObject 是大多数 Objective-C 类的基类,提供了许多基础的方法和属性。大多数自定义类都会继承自 NSObject。

@interface MyClass : NSObject
// ...
@end
  1. NSString

NSString 是不可变的字符串类,用于表示文本。

NSString *string = @"Hello, World!";
  1. NSMutableString

NSMutableString 是可变的字符串类,用于需要修改字符串内容的情况。

NSMutableString *mutableString = [NSMutableString stringWithString:@"Hello"];
[mutableString appendString:@", World!"];
  1. NSArray

NSArray 是不可变的数组类,用于存储有序的对象集合。

NSArray *array = @[@"Apple", @"Banana", @"Cherry"];
  1. NSMutableArray

NSMutableArray 是可变的数组类,允许在数组中添加或移除元素。

NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:@[@"Apple", @"Banana"]];
[mutableArray addObject:@"Cherry"];
  1. NSDictionary

NSDictionary 是不可变的字典类,用于存储键值对。

NSDictionary *dictionary = @{@"name": @"John", @"age": @30};
  1. NSMutableDictionary

NSMutableDictionary 是可变的字典类,允许在字典中添加或移除键值对。

NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:@{@"name": @"John"}];
[mutableDictionary setObject:@30 forKey:@"age"];
  1. NSNumber

NSNumber 是用于封装基本数据类型(如 int、float 等)的对象类型。

NSNumber *number = @42;
NSNumber *floatNumber = @3.14;
  1. NSValue

NSValue 是用于封装非对象数据类型(如结构体、指针等)的对象类型。

NSRect rect = NSMakeRect(0, 0, 100, 100);
NSValue *rectValue = [NSValue valueWithRect:rect];

常用类

  1. NSDate

NSDate 是用于表示日期和时间的类。

NSDate *date = [NSDate date];
  1. NSData

NSData 是用于表示任意二进制数据的类。

NSData *data = [NSData dataWithContentsOfFile:@"path/to/file"];
  1. NSSet

NSSet 是用于存储唯一对象的无序集合。

NSSet *set = [NSSet setWithObjects:@"Apple", @"Banana", @"Cherry", nil];
  1. NSMutableSet

NSMutableSet 是可变的无序集合,允许添加和移除对象。

NSMutableSet *mutableSet = [NSMutableSet setWithObjects:@"Apple", @"Banana", nil];
[mutableSet addObject:@"Cherry"];

动态类型

  1. id

id 是一种动态类型,可以指向任何 Objective-C 对象。

id obj = @"Hello, World!";