When Apple released Xcode7, it not only upgraded the SWIFT programming language to version 2.0, but also made a number of upgrades to objective-c, including the introduction of __nonnull/__nullable. Among them, for the OBJECTIVE-C programming language itself, more useful is the lightweight generics.
Among them, the more obvious embodiment is nsarray, nsdictionary these container classes all adopt the newly introduced lightweight generics. With lightweight generics, we can easily get the elements in them and access the properties and methods that are unique to their printing. Let's give a simple example to illustrate the convenience of lightweight threading:
Non-generic situation nsarray *numarray = @[@10, @20, @30]; int sum = [(nsnumber*) numarray[0] intvalue] + [(nsnumber*) numarray[1] intvalue] + [(nsnumber*) numarray[2] intvalue];
Use generics in the case of nsarray<nsnumber*> *numarray = @[@10, @20, @30]; int sum = [Numarray[0] intvalue] + [numarray[1] intvalue] + [numarray[2] intvalue];
We can see from the above examples the syntactic convenience of lightweight generics, which is a sweet syntactic sugar (syntax sugar).
The previous Apple LLVM 6.0 C11 standard generic--generic selection was not well supported on the objective-c, but Apple LLVM 7.0 has the perfect support. For example, the following examples:
int flag = _generic (@100, Nsnumber*:1, Nsstring*:2, Int:3, default:0);
NSLog (@ "The flag is:%d", flag);
The above code will successfully output "the flag is:1".
Unlike the generic selection of C11, Objective-c's own generics are covariant type, or covariant type. That is, its generics are somewhat similar to those of Java. It requires that the generic must be a objective-c class type, that is, at least the ID type. For the above Nsarray example, when declaring a Objective-c object reference, add <NSNumber*> to the class name to indicate that the element in the current Nsarray is a nsnumber* class or its subclass type. In this way, we can access its Intvalue method directly when accessing its elements.
Here's how to define a generic class yourself. Its syntax is described as follows:
@interface class_name < __covariant type_identifier > Inherit_expression
Here, the class_name is the class name; Type_identifier is the type identifier, which can be named by the programmer, and the last inherit_expression represents inheriting a parent class and/or implementing some protocols.
This introduces a new keyword--__covariant, which means that the type_identifier behind is a generic type. The generic type is specified when declaring an object.
Here's a concrete example to illustrate how to use the Objective-c generics specifically.
@interface myobject<__covariant t>: NSObject
{
@private
T obj;
}
@property (nonatomic, retain) T obj;
@end
@implementation MyObject
@synthesize obj;
-(void) Dealloc
{
if (obj!= nil)
[obj release];
NSLog (@ "My object deallocated!");
[Super Dealloc];
}
@end
@implementation Viewcontroller
-(void) viewdidload {
myobject<nsnumber*> *numobj = [ MyObject alloc] init];
Numobj.obj = @100;
[Numobj release];
myobject<nsstring*> *strobj = [[MyObject alloc] init];
Strobj.obj = @ "Hello, world";
[Strobj release];
}
@end
In the above code, we define a MyObject generic class whose generic identifier is represented by T. We then used the generic t to define a private object, obj, and use it as a property.
Subsequently, we declared an object numobj with myobject<nsnumber*> in the Viewdidload method; A Strobj object was declared with myobject<nsstring*>. We can call the int value directly through the Numobj.obj calls Intvalue, and call the length method directly through Strobj.obj to get its string length//without generics nsarray *numarray = @[@ 10, @ 20, @ 30]; int sum = [(nsnumber*) numarray[0] intvalue] + [(nsnumber*) numarray[1] intvalue] + [(nsnumber*) numarray[2] intvalue]; Using generics nsarray<nsnumber*> *numarray = @[@ 10, @ 20, @ 30]; int sum = [numarray[0] intvalue] + [numarray[1] intvalue] + [numarray[2] intvalue];