1 重構學生與學校的練習
1.1 問題
本案例要求用字典解決下述問題。問題是:有一個學校,該學校有兩個學院,每個學院中又有兩個班級,而在每個班級中有兩名學生。
現在作如下要求:
1)顯示所有學生的信息。
2)只顯示姓名為“zhangsan”的學生的信息。
3)只顯示年齡為18歲的學生的信息。
4)如果每個學生有本書,顯示所有學生的書的名稱和價格。
1.2 步驟
實現此案例需要按照如下步驟進行。
步驟一:定義類Book
首先,在Day04工程中新添加Book.h文件,用于定義新的類Book。
代碼如下所示:
? #import <Foundation/Foundation.h> @interface Book : NSObject<NSCopying> @property(nonatomic, copy)NSString *name;@property(nonatomic,assign)int price; @end 在上述代碼中,以下代碼:
? @interface Book : NSObject<NSCopying> 定義了類Book,該類采用了NSCopying協議。
在上述代碼中,以下代碼:
@property(nonatomic, copy)NSString *name; 在Book類中定義的屬性name是NSString類的對象,用于存儲書的名字。它有兩個參數,一個是nonatomic,它代表對屬性賦值的時候不加鎖,即在多線程環境下訪問時可能會出現數據錯誤,如果需要在多線程環境下運行,為保證數據不會出現錯誤,可使用atomic參數,它會在對屬性賦值的時候加鎖。另一個參數是copy,該參數一般用于NSObject類及其子類的對象,這些對象在賦值時實現深拷貝,即屬性name指向的對象是賦值給它的對象的副本。
在上述代碼中,以下代碼:
@property(nonatomic,assign)int price; 在Book類中定義的整型屬性price,用于存儲書的價格。它有兩個參數,一個是nonatomic,另一個參數是assign,對于C語言的基本數據類型,只能選取這個參數。
然后,在類Book的實現部分,即在Book.m文件中,添加兩個方法的實現,一個是copyWithZone方法,該方法是在NSCopying協議中聲明的方法,用于深拷貝;另一個是dealloc方法,由于使用ARC,該方法在本類中實際上沒有任何實際意義,只是在類的對象銷毀時,在控制臺上輸出一個提示。
代碼如下所示:
#import "Book.h" @implementation Book -(id)copyWithZone:(NSZone*)zone{Book* book = [[Book allocWithZone:zone] init];book.name = self.name;book.price = self.price;return book;} -(void)dealloc{NSLog(@"書對象銷毀了 price:%d",self.price);} @end 上述代碼中,copyWithZone方法不能在類外直接被調用,它是在向一個對象發送copy消息時,由copy消息調用。在該方法中,以下代碼:
Book* book = [[Book allocWithZone:zone] init];book.name = self.name;book.price = self.price; 生成一個本類對象的副本。需要注意的是分配空間使用的是allocWithZone方法。
上述代碼中,dealloc方法也不能在類外直接被調用,它是在一個對象被release并且對象的引用計數為0時,由內存管理程序調用的方法。
步驟二:定義類TRStudent
首先,在Day04工程中新添加TRStudent.h文件,用于定義新的類TRStudent。
代碼如下所示:
#import <Foundation/Foundation.h>#import "Book.h" @interface TRStudent : NSObject @property(nonatomic,assign)int age;@property(nonatomic,copy)NSString* name;@property(nonatomic,copy)Book *book; -(id)initWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;+(id)studentWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;-(void)print:(id)condition; @end 在上述代碼中,定義了類Student,在類中有三個帶參屬性。
一個屬性是整型變量age,如下代碼所示:
@property(nonatomic,assign) int age; 用于存儲學生的年齡。
另一個屬性是name,如下代碼所示:
@property(nonatomic,copy)NSString* name; 用于存儲學生的姓名。
最后一個屬性是book,如下代碼所示:
@property(nonatomic,copy)Book *book; 用于存儲學生正在學習的書。值得注意的是,Book類采納NSCopying協議,是因為此處使用了copy這個屬性參數。
上述代碼中,以下代碼:
-(id)initWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;+(id)studentWithAge:(int)age andName:(NSString*)name andBook:(Book*)book; 聲明了TRStudent類的帶參初始化方法和帶參工廠方法。
上述代碼中,以下代碼:
-(void)print:(id)condition; 聲明了一個print方法,該方法用于根據形參condition的不同,打印不同的TRStudent類對象的屬性值。
然后,在類Student的實現部分,即在Student.m文件中,添加帶參初始化方法和工廠初始化方法以及print方法的實現。
代碼如下所示:
#import "TRStudent.h" @implementation TRStudent -(id)initWithAge:(int)ageandName:(NSString*)nameandBook:(Book*)book{self = [super init];if (self) {self.age = age;self.name = name;self.book = book;}return self;} +(id)studentWithAge:(int)ageandName:(NSString*)nameandBook:(Book*)book{return [[TRStudent alloc]initWithAge:age andName:name andBook:book];} -(void)print:(id)condition{bool a = [condition isKindOfClass:[NSNumber class]];bool b = [condition intValue] == self.age;bool c = [condition isKindOfClass:[NSString class]];bool d = [self.name isEqualToString:condition];if ((a && b) || (c && d))NSLog(@"stu name:%@, age:%d", self.name, self.age);} @end 上述代碼中,以下代碼:
bool a = [condition isKindOfClass:[NSNumber class]];bool b = [condition intValue] == self.age; 定義了兩個bool類型的變量。
變量a被賦值為形參condition是否是NSNumber類對象的判斷結果,其中方法isKindOfClass的作用是判斷對象condition是否是NSNumber類的對象。方法isKindOfClass的形參為Class類型的值,該類型的值通過工廠方法class獲得。
變量b被賦值為形參condition的值是否與當前對象age屬性值相等的判斷結果。通過向形參condition發送intValue消息,如果condition的值為數字字符串,則將其轉換成整型數據。
上述代碼中,以下代碼:
bool c = [condition isKindOfClass:[NSString class]];bool d = [self.name isEqualToString:condition]; 定義了兩個bool類型的變量。
變量c被賦值為形參condition是否是NSString類對象的判斷結果。
變量d被賦值為當前對象name屬性值是否與形參condition的值相等的判斷結果。方法isEqualToString是NSString類的方法,用于判斷兩個字符串是否相等。
上述代碼中,以下代碼:
if ((a && b) || (c && d))NSLog(@"stu name:%@, age:%d", self.name, self.age); 如果條件(a && b)為真則表示形參condition為NSNumber類的對象并且將其拆箱后的值與當前對象age屬性值相等。
如果條件(c && d)為真則表示形參condition為NSString類的對象并且與當前對象name屬性值是同一字符串。
步驟三:在主程序中創建八個學生對象
代碼如下所示:
#import <Foundation/Foundation.h>#import "TRStudent.h" int main(int argc, const char * argv[]){ @autoreleasepool { Book* book1 = [[Book alloc] init];book1.name = @"sanguo";book1.price = 20;TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1]; Book* book2 = [[Book alloc] init];book2.name = @"shuihu";book2.price = 18;TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2]; Book* book3 = [[Book alloc] init];book3.name = @"xiyouji";book3.price = 28;TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3]; Book* book4 = [[Book alloc] init];book4.name = @"hongluomeng";book4.price = 24;TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4]; Book* book5 = [[Book alloc] init];book5.name = @"fengshenyanyi";book5.price = 22;TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5]; Book* book6 = [[Book alloc] init];book6.name = @"liaozhaizhiyi";book6.price = 15;TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6]; Book* book7 = [[Book alloc] init];book7.name = @"sanxiawuyi";book7.price = 17;TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7]; Book* book8 = [[Book alloc] init];book8.name = @"yuefeizhuan";book8.price = 27;TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8]; }return 0;} 上述代碼中,以下代碼:
Book* book1 = [[Book alloc] init];book1.name = @"sanguo";book1.price = 20;TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1]; 首先創建了一個Book類的對象book1,然后使用工廠方法studentWithAge:andName:andBook:創建TRStudent類的對象stu1。
步驟四:在主程序中創建班級字典
代碼如下所示:
#import <Foundation/Foundation.h>#import "TRStudent.h" int main(int argc, const char * argv[]){ @autoreleasepool { Book* book1 = [[Book alloc] init];book1.name = @"sanguo";book1.price = 20;TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1]; Book* book2 = [[Book alloc] init];book2.name = @"shuihu";book2.price = 18;TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2]; Book* book3 = [[Book alloc] init];book3.name = @"xiyouji";book3.price = 28;TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3]; Book* book4 = [[Book alloc] init];book4.name = @"hongluomeng";book4.price = 24;TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4]; Book* book5 = [[Book alloc] init];book5.name = @"fengshenyanyi";book5.price = 22;TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5]; Book* book6 = [[Book alloc] init];book6.name = @"liaozhaizhiyi";book6.price = 15;TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6]; Book* book7 = [[Book alloc] init];book7.name = @"sanxiawuyi";book7.price = 17;TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7]; Book* book8 = [[Book alloc] init];book8.name = @"yuefeizhuan";book8.price = 27;TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8]; NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil]; }return 0;} 上述代碼中,以下代碼:
NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil]; 創建了四個字典對象,每個字典對象為一個班級,班級中有兩個學生作為字典的值value,并為其定義了編號@“1”和@“2”作為關鍵字key。其中使用工廠方法dictionaryWithObjectsAndKeys:創建字典對象。值得注意的是,在形參的最后是nil,它代表放入字典中的鍵值對結束。
步驟五:在主程序中創建學院和學校
代碼如下所示:
#import <Foundation/Foundation.h>#import "TRStudent.h" int main(int argc, const char * argv[]){ @autoreleasepool { Book* book1 = [[Book alloc] init];book1.name = @"sanguo";book1.price = 20;TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1]; Book* book2 = [[Book alloc] init];book2.name = @"shuihu";book2.price = 18;TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2]; Book* book3 = [[Book alloc] init];book3.name = @"xiyouji";book3.price = 28;TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3]; Book* book4 = [[Book alloc] init];book4.name = @"hongluomeng";book4.price = 24;TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4]; Book* book5 = [[Book alloc] init];book5.name = @"fengshenyanyi";book5.price = 22;TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5]; Book* book6 = [[Book alloc] init];book6.name = @"liaozhaizhiyi";book6.price = 15;TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6]; Book* book7 = [[Book alloc] init];book7.name = @"sanxiawuyi";book7.price = 17;TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7]; Book* book8 = [[Book alloc] init];book8.name = @"yuefeizhuan";book8.price = 27;TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8]; NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil]; NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil]; NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil]; }return 0;} 上述代碼中,以下代碼:
NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil]; NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil]; 創建了兩個學院字典,每個學院字典中有兩個班級字典作為字典的值value,并為其定義了編號@“1”和@“2”作為關鍵字key。還創建了一個學校字典,其中包含上述兩個學院字典作為字典的值value,并為其定義了編號@“1”和@“2”作為關鍵字key。
步驟六:在主程序中遍歷輸出所有學生信息
代碼如下所示:
#import <Foundation/Foundation.h>#import "TRStudent.h" int main(int argc, const char * argv[]){ @autoreleasepool { Book* book1 = [[Book alloc] init];book1.name = @"sanguo";book1.price = 20;TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1]; Book* book2 = [[Book alloc] init];book2.name = @"shuihu";book2.price = 18;TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2]; Book* book3 = [[Book alloc] init];book3.name = @"xiyouji";book3.price = 28;TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3]; Book* book4 = [[Book alloc] init];book4.name = @"hongluomeng";book4.price = 24;TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4]; Book* book5 = [[Book alloc] init];book5.name = @"fengshenyanyi";book5.price = 22;TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5]; Book* book6 = [[Book alloc] init];book6.name = @"liaozhaizhiyi";book6.price = 15;TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6]; Book* book7 = [[Book alloc] init];book7.name = @"sanxiawuyi";book7.price = 17;TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7]; Book* book8 = [[Book alloc] init];book8.name = @"yuefeizhuan";book8.price = 27;TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8]; NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil]; NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil]; NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil]; NSEnumerator *e = [tarena objectEnumerator];NSDictionary* college;while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){NSLog(@"stu name:%@, age:%d", stu.name, stu.age);}}} }return 0;} 上述代碼中,以下代碼:
NSEnumerator *e = [tarena objectEnumerator];NSDictionary* college;while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){NSLog(@"stu name:%@, age:%d", stu.name, stu.age);}}} 通過三重枚舉遍歷方法遍歷所有學生的信息。以下代碼:
NSEnumerator *e = [tarena objectEnumerator]; 是通過NSDictionary類的objectEnumerator方法,獲取學校tarena的值value的枚舉器。
以下代碼:
NSDictionary* college;while (college = [e nextObject]) 是通過NSEnumerator類的nextObject方法,使用循環逐個獲取學校tarena中的每一個值value對象。
以下代碼:
NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]) 是通過建立學院college的值value枚舉器,使用循環逐個獲得每一個值value對象。
以下代碼:
NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]) 是通過建立班級class的值value枚舉器,使用循環逐個獲得每一個值value對象。
步驟七:在主程序中遍歷顯示指定條件的學生信息
代碼如下所示:
#import <Foundation/Foundation.h>#import "TRStudent.h" int main(int argc, const char * argv[]){ @autoreleasepool { Book* book1 = [[Book alloc] init];book1.name = @"sanguo";book1.price = 20;TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1]; Book* book2 = [[Book alloc] init];book2.name = @"shuihu";book2.price = 18;TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2]; Book* book3 = [[Book alloc] init];book3.name = @"xiyouji";book3.price = 28;TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3]; Book* book4 = [[Book alloc] init];book4.name = @"hongluomeng";book4.price = 24;TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4]; Book* book5 = [[Book alloc] init];book5.name = @"fengshenyanyi";book5.price = 22;TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5]; Book* book6 = [[Book alloc] init];book6.name = @"liaozhaizhiyi";book6.price = 15;TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6]; Book* book7 = [[Book alloc] init];book7.name = @"sanxiawuyi";book7.price = 17;TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7]; Book* book8 = [[Book alloc] init];book8.name = @"yuefeizhuan";book8.price = 27;TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8]; NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil]; NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil]; NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil]; NSEnumerator *e = [tarena objectEnumerator];NSDictionary* college;while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){NSLog(@"stu name:%@, age:%d", stu.name, stu.age);}}} e = [tarena objectEnumerator];while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){[stu print:[NSNumber numberWithInt:18]];[stu print:@"zhangsan"];}}}}return 0;} 上述代碼中,以下代碼:
[stu print:[NSNumber numberWithInt:18]];[stu print:@"zhangsan"]; 調用TRSTudent的print:方法,有選擇的打印對象stu的信息。print:方法有一個實參[NSNumber numberWithInt:18]作為選擇的條件,如若條件成立,則打印對象stu的屬性。
步驟八:在主程序中顯示所有學生的書的名稱和價格
代碼如下所示:
#import <Foundation/Foundation.h>#import "TRStudent.h" int main(int argc, const char * argv[]){ @autoreleasepool { Book* book1 = [[Book alloc] init];book1.name = @"sanguo";book1.price = 20;TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1]; Book* book2 = [[Book alloc] init];book2.name = @"shuihu";book2.price = 18;TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2]; Book* book3 = [[Book alloc] init];book3.name = @"xiyouji";book3.price = 28;TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3]; Book* book4 = [[Book alloc] init];book4.name = @"hongluomeng";book4.price = 24;TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4]; Book* book5 = [[Book alloc] init];book5.name = @"fengshenyanyi";book5.price = 22;TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5]; Book* book6 = [[Book alloc] init];book6.name = @"liaozhaizhiyi";book6.price = 15;TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6]; Book* book7 = [[Book alloc] init];book7.name = @"sanxiawuyi";book7.price = 17;TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7]; Book* book8 = [[Book alloc] init];book8.name = @"yuefeizhuan";book8.price = 27;TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8]; NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil]; NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil]; NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil]; NSEnumerator *e = [tarena objectEnumerator];NSDictionary* college;while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){NSLog(@"stu name:%@, age:%d", stu.name, stu.age);}}} e = [tarena objectEnumerator];while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){[stu print:[NSNumber numberWithInt:18]];[stu print:@"zhangsan"];}}} e = [tarena objectEnumerator];while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){NSLog(@"book name:%@, price:%d", stu.book.name, stu.book.price);}}}}return 0;} 上述代碼再一次使用了三重枚舉遍歷,這一次與步驟六(遍歷輸出所有學生信息)中的三重枚舉遍歷相比,區別是最后使用NSLog輸出為書的名稱和價格,不是學生的姓名和年齡。
1.3 完整代碼
本案例中,類Book聲明,即Book.h文件,完整代碼如下所示:
#import <Foundation/Foundation.h> @interface Book : NSObject<NSCopying> @property(nonatomic, copy)NSString *name;@property(nonatomic,assign)int price; @end 類Book實現,即Book.m文件,完整代碼如下所示:
#import "Book.h" @implementation Book -(id)copyWithZone:(NSZone*)zone{Book* book = [[Book allocWithZone:zone] init];book.name = self.name;book.price = self.price;return book;} -(void)dealloc{NSLog(@"書對象銷毀了 price:%d",self.price);} @end 本案例中,類TRStudent聲明,即TRStudent.h文件,完整代碼如下所示:
#import <Foundation/Foundation.h>#import "Book.h" @interface TRStudent : NSObject @property(nonatomic,assign)int age;@property(nonatomic,copy)NSString* name;@property(nonatomic,copy)Book *book; -(id)initWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;+(id)studentWithAge:(int)age andName:(NSString*)name andBook:(Book*)book;-(void)print:(id)condition; @end 類TRStudent實現,即TRStudent.m文件,完整代碼如下所示:
#import "TRStudent.h" @implementation TRStudent -(id)initWithAge:(int)ageandName:(NSString*)nameandBook:(Book*)book{self = [super init];if (self) {self.age = age;self.name = name;self.book = book;}return self;} +(id)studentWithAge:(int)ageandName:(NSString*)nameandBook:(Book*)book{return [[TRStudent alloc]initWithAge:age andName:name andBook:book];} -(void)print:(id)condition{bool a = [condition isKindOfClass:[NSNumber class]];bool b = [condition intValue] == self.age;bool c = [condition isKindOfClass:[NSString class]];bool d = [self.name isEqualToString:condition];if ((a && b) || (c && d))NSLog(@"stu name:%@, age:%d", self.name, self.age);} @end 主程序,即main.m,完整代碼如下所示:
#import <Foundation/Foundation.h>#import "TRStudent.h" int main(int argc, const char * argv[]){ @autoreleasepool { Book* book1 = [[Book alloc] init];book1.name = @"sanguo";book1.price = 20;TRStudent* stu1 = [TRStudent studentWithAge:18 andName:@"zhangsan" andBook:book1]; Book* book2 = [[Book alloc] init];book2.name = @"shuihu";book2.price = 18;TRStudent* stu2 = [TRStudent studentWithAge:19 andName:@"lisi" andBook:book2]; Book* book3 = [[Book alloc] init];book3.name = @"xiyouji";book3.price = 28;TRStudent* stu3 = [TRStudent studentWithAge:20 andName:@"wangwu" andBook:book3]; Book* book4 = [[Book alloc] init];book4.name = @"hongluomeng";book4.price = 24;TRStudent* stu4 = [TRStudent studentWithAge:21 andName:@"zhaoliu" andBook:book4]; Book* book5 = [[Book alloc] init];book5.name = @"fengshenyanyi";book5.price = 22;TRStudent* stu5 = [TRStudent studentWithAge:22 andName:@"qianqi" andBook:book5]; Book* book6 = [[Book alloc] init];book6.name = @"liaozhaizhiyi";book6.price = 15;TRStudent* stu6 = [TRStudent studentWithAge:23 andName:@"zhangfei" andBook:book6]; Book* book7 = [[Book alloc] init];book7.name = @"sanxiawuyi";book7.price = 17;TRStudent* stu7 = [TRStudent studentWithAge:24 andName:@"guanyu" andBook:book7]; Book* book8 = [[Book alloc] init];book8.name = @"yuefeizhuan";book8.price = 27;TRStudent* stu8 = [TRStudent studentWithAge:25 andName:@"zhaoyun" andBook:book8]; NSDictionary* class1403A = [NSDictionary dictionaryWithObjectsAndKeys:stu1, @"1", stu2, @"2", nil];NSDictionary* class1403B = [NSDictionary dictionaryWithObjectsAndKeys:stu3, @"1", stu4, @"2", nil];NSDictionary* class1403C = [NSDictionary dictionaryWithObjectsAndKeys:stu5, @"1", stu6, @"2", nil];NSDictionary* class1403D = [NSDictionary dictionaryWithObjectsAndKeys:stu7, @"1", stu8, @"2", nil]; NSDictionary* college3G = [NSDictionary dictionaryWithObjectsAndKeys:class1403A, @"1", class1403B, @"2", nil];NSDictionary* collegeTest = [NSDictionary dictionaryWithObjectsAndKeys:class1403C, @"1", class1403D, @"2", nil]; NSDictionary* tarena = [NSDictionary dictionaryWithObjectsAndKeys:college3G, @"1", collegeTest, @"2", nil]; NSEnumerator *e = [tarena objectEnumerator];NSDictionary* college;while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){NSLog(@"stu name:%@, age:%d", stu.name, stu.age);}}} e = [tarena objectEnumerator];while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){[stu print:[NSNumber numberWithInt:18]];[stu print:@"zhangsan"];}}} e = [tarena objectEnumerator];while (college = [e nextObject]){NSEnumerator *e1 = [college objectEnumerator];NSDictionary* class;while (class = [e1 nextObject]){NSEnumerator *e2 = [class objectEnumerator];TRStudent* stu;while (stu = [e2 nextObject]){NSLog(@"book name:%@, price:%d", stu.book.name, stu.book.price);}}}}return 0;}
轉載于:https://www.cnblogs.com/52190112cn/p/5049292.html
總結
以上是生活随笔為你收集整理的NSDictionary 、 NSMutableDictionary的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。