#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; Class clusters are a widely used design pattern in the Foundation framework. Class clusters group private, concrete subclasses under a common, abstract superclass. Organizing classes in this way simplifies the public architecture of an object-oriented framework without reducing the richness of functionality. [self testArray]; [self testDictionary]; [self testString]; } - (void)testArray { NSLog(@"--------------------------------------------------------------"); NSArray *a1 = [NSArray alloc]; NSArray *a2 = [[NSArray alloc] init]; NSArray *a3 = [NSArray array]; NSArray *a4 = [NSArray arrayWithObject:@"1"]; NSArray *a5 = [NSArray arrayWithObjects:@"1",@"2",nil]; NSLog(@"\na1:%@, \na2:%@, \na3:%@, \na4:%@, \na5:%@",[a1 class],[a2 class],[a3 class],[a4 class],[a5 class]); NSLog(@"-------------depart line--------------"); NSMutableArray *m1 = [NSMutableArray alloc]; NSMutableArray *m2 = [[NSMutableArray alloc] init]; NSMutableArray *m3 = [NSMutableArray array]; NSMutableArray *m4 = [NSMutableArray arrayWithObject:@"1"]; NSMutableArray *m5 = [NSMutableArray arrayWithObjects:@"1",@"2",nil]; NSMutableArray *m6 = a5.mutableCopy; NSLog(@"\nm1:%@, \nm2:%@, \nm3:%@, \nm4:%@, \nm5:%@, \nm6:%@",[m1 class],[m2 class],[m3 class],[m4 class],[m5 class],[m6 class]); } - (void)testDictionary { NSLog(@"--------------------------------------------------------------"); NSDictionary *d1 = [NSDictionary alloc]; NSDictionary *d2 = [[NSDictionary alloc] init]; NSDictionary *d3 = [NSDictionary dictionary]; NSDictionary *d4 = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"]; NSDictionary *d5 = @{@"k1":@"v1", @"k2":@"v2"}; NSLog(@"\nd1:%@, \nd2:%@, \nd3:%@, \nd4:%@, \nd5:%@",[d1 class],[d2 class],[d3 class],[d4 class],[d5 class]); NSLog(@"-------------depart line--------------"); NSMutableDictionary *md1 = [NSMutableDictionary alloc]; NSMutableDictionary *md2 = [[NSMutableDictionary alloc] init]; NSMutableDictionary *md3 = [NSMutableDictionary dictionaryWithObject:@"value" forKey:@"key"]; NSMutableDictionary *md4 = [NSMutableDictionary dictionaryWithDictionary:d5]; NSMutableDictionary *md5 = @{@"k1":@"v1", @"k2":@"v2"}.mutableCopy; NSLog(@"\nmd1:%@, \nmd2:%@, \nmd3:%@, \nmd4:%@, \nmd5:%@",[md1 class],[md2 class],[md3 class],[md4 class],[md5 class]); } - (void)testString { NSLog(@"--------------------------------------------------------------"); NSString *s1 = [NSString alloc]; NSString *s2 = [[NSString alloc] init]; NSString *s3 = @"test"; NSString *s4 = [NSString stringWithFormat:@"testforstringWithFormat"]; NSString *s5 = [NSString stringWithUTF8String:"test"]; NSString *s6 = [NSString stringWithUTF8String:"中文"]; NSLog(@"\ns1:%@, \ns2:%@, \ns3:%@, \ns4:%@, \ns5:%@, \ns6:%@",[s1 class],[s2 class],[s3 class],[s4 class],[s5 class],[s6 class]); NSLog(@"-------------depart line--------------"); /* care:s5 is of type NSTaggedPointerString; S6 is of type __NSCFString 1, NSTaggedPointerString its parent is NSString 2, __NSTaggedPointerString is a label pointer string, These are some of the optimizations Apple has made for NSString,NSNumber, etc in 64-bit environments. In simple terms, this means that the pointer is placed directly in the memory address of the pointer variable, since the size of the pointer variable is 8 bits large enough to hold something of small length in a 64-bit environment. So the label pointer is used to optimize the way data is stored. As you can see from his reference count, this object is also a singleton constant object that cannot be freed. Created at run time as needed. NSTaggedPointerString = NSTaggedPointerString = NSTaggedPointerString = NSTaggedPointerString; __NSCFString if Chinese or other special characters (possibly non-ASCII characters) exist. This object is stored directly in the contents of the pointer and can be treated as a pseudo-object. (so: s6 type is __NSCFString) */ NSMutableString *ms1 = [NSMutableString alloc]; NSMutableString *ms2 = @""; NSMutableString *ms3 = [[NSMutableString alloc] init]; NSMutableString *ms4 = [NSMutableString stringWithFormat:@"test"]; NSLog(@"\nms1:%@, \nms2:%@, \nms3:%@, \nsm4:%@",[ms1 class],[ms2 class],[ms3 class],[ms4 class]); } @endCopy the code
Therefore, direct alloc may cause a program to crash. NSNotification *notif = [[NSNotification alloc]init]; This raises the following exception.
*** Terminating app due to uncaught exception ‘NSGenericException’, reason: ‘*** -[NSConcreteNotification init]: should never be used’ terminating with uncaught exception of type NSException