Code:
Main
#import<Foundation/Foundation.h>#import "Student.h"intMainintargcConst Char*argv[]) {@autoreleasepool {Student*stu1=[[student Alloc]initwithname:@"Wang"Andgender:@"male"andage:@ -]; Student*stu2=[[student Alloc]initwithname:@"Yang"Andgender:@"female"andage:@ -]; Student*stu3=[[student Alloc]initwithname:@"Lang"Andgender:@"male"andage:@ -]; Nsdictionary*dic1=[nsdictionary DictionaryWithObjectsAndKeys:stu1.name,@"name", Stu1.gender,@"Gender", Stu1.age,@" Age", nil]; Nsdictionary*dic2=[nsdictionary DictionaryWithObjectsAndKeys:stu2.name,@"name", Stu2.gender,@"Gender", Stu2.age,@" Age", nil]; Nsdictionary*dic3=[nsdictionary DictionaryWithObjectsAndKeys:stu3.name,@"name", Stu3.gender,@"Gender", Stu3.age,@" Age", nil]; Nsarray*arr=[Nsarray arraywithobjects:dic1,dic2,dic3, Nil]; [Student Displayfirstandlastname:arr]; [Student Caculatefirstandlastage:arr]; } return 0;}
Student.h:
#import <Foundation/Foundation.h>@interface student:nsobject-(instancetype) Initwithname: (NSString *) newName Andgender: (NSString *) Newgender andage: (NSNumber *) newage; + (void) Displayfirstandlastname: (Nsarray *) array; + (void) Caculatefirstandlastage: (Nsarray ** * * age; @end
STUDENT.M:
#import "Student.h"@implementationStudent-(Instancetype) Initwithname: (NSString *) newName Andgender: (NSString *) Newgender andage: (NSNumber *) newage{name=NewName; Gender=Newgender; Age=NewAge; returnSelf ;}+(void) Displayfirstandlastname: (Nsarray *) array{nsdictionary*dicfirst=[Array firstobject]; Nsdictionary*diclast=[Array lastobject]; NSLog (@"First name:%@,last name:%@", dicfirst[@"name"],diclast[@"name"]);}+(void) Caculatefirstandlastage: (Nsarray *) array{nsdictionary*dicfirst=[Array firstobject]; Nsdictionary*diclast=[Array lastobject]; intfirstage=[dicfirst[@" Age"] intvalue]; intlastage=[diclast[@" Age"] intvalue]; NSLog (@"Age between first and last:%d", ABS (firstage-lastage));}@synthesizeName,gender,age;@end
[Example] creates a student class, enters student information into a dictionary, stores 3 dictionaries of student information in an array, and calculates