Object-C: 学习实例3 使用集合对象
2013-11-20 09:27
344 查看
Exam_03_Impl.h
//
// Exam_03_Impl.h
// Exam_03
//
// Created by yao_yu on 13-11-19.
// Copyright (c) 2013年 yao_yu. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(strong)NSString *firstName;
@property(strong)NSString *lastName;
@property(assign) int age;
-(id)initWithFirstName:(NSString *)fName lastName:(NSString *)lName andAge:(int)a;
-(void)reportState;
@end
void Exam_03_01();
void Exam_03_02();
void Exam_03_05();
void Exam_03_10();
Exam_03_Impl.m
//
// Exam_03_Impl.m
// 使用对象集合
//
// Created by yao_yu on 13-11-19.
// Copyright (c) 2013年 yao_yu. All rights reserved.
//
#import "Exam_03_Impl.h"
void Exam_03_01()
{
@autoreleasepool {
NSArray *listOfLetters1 = [NSArray arrayWithObjects:@"A", @"B", @"C", nil];
NSLog(@"listOfLetters1 = %@", listOfLetters1);
NSNumber *n1 = [NSNumber numberWithInt: 1];
NSNumber *n2 = [NSNumber numberWithInt: 2];
NSNumber *n3 = [NSNumber numberWithInt: 3];
NSMutableArray *list2 = [[NSMutableArray alloc] initWithObjects: n1, n2, n3, nil];
NSLog(@"list2 = %@", list2);
id list[3];
list[0] = @"D";
list[1] = @"E";
list[2] = @"F";
NSMutableArray *list3 = [[NSMutableArray alloc] initWithObjects:list count:3];
NSLog(@"list3 = %@", list3);
}
}
void Exam_03_02()
{
@autoreleasepool {
NSMutableArray *ma = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString: @"A"],
[NSMutableString stringWithString: @"B"],
[NSMutableString stringWithString: @"C"],
[NSMutableString stringWithString: @"D"],
nil];
NSString *s1 = [ma objectAtIndex: 0];
NSLog(@"s1 = %@", s1);
NSString *s2 = [ma objectAtIndex: 1];
NSLog(@"S2 = %@", s2);
NSUInteger p = [ma indexOfObject: @"B"];
NSLog(@"位置 = %lu", p);
NSLog(@"数组元素个数 = %lu", ma.count);
for(NSMutableString *s in ma)
{
NSLog(@"字符串小写是 %@", [s lowercaseString]);
}
[ma makeObjectsPerformSelector:@selector(appendString:) withObject:@"-MORE"];
[ma enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"对象(%lu)的描述为%@", idx, [obj description]);
}];
}
}
@implementation Person
@synthesize firstName, lastName, age;
-(id)initWithFirstName:(NSString *)fName lastName:(NSString *)lName andAge:(int)a
{
self = [super init];
if(self){
self.firstName = fName;
self.lastName = lName;
self.age = a;
}
return self;
}
-(void)reportState
{
NSLog(@"我叫%@%@, 今年%i岁", self.firstName, self.lastName, self.age);
}
@end
void Exam_03_05_Sort(NSArray *la, NSSortDescriptor *sd1, NSSortDescriptor *sd2, NSSortDescriptor *sd3)
{
NSLog(@"按[%@,%@,%@]排序后的数组", [sd1 key], [sd2 key], [sd3 key]);
NSArray *sda1 = [NSArray arrayWithObjects:sd1, sd2, sd3, nil];
NSArray *sortedArray1 = [la sortedArrayUsingDescriptors: sda1];
[sortedArray1 makeObjectsPerformSelector:@selector(reportState)];
}
//数组排序,过滤
void Exam_03_05()
{
@autoreleasepool {
Person *p1 = [[Person alloc] initWithFirstName:@"张" lastName:@"三" andAge:33];
Person *p2 = [[Person alloc] initWithFirstName:@"李" lastName:@"四" andAge:24];
Person *p3 = [[Person alloc] initWithFirstName:@"王" lastName:@"五" andAge:45];
Person *p4 = [[Person alloc] initWithFirstName:@"赵" lastName:@"大" andAge:35];
Person *p5 = [[Person alloc] initWithFirstName:@"钱" lastName:@"小" andAge:32];
Person *p6 = [[Person alloc] initWithFirstName:@"孙" lastName:@"武" andAge:21];
Person *p7 = [[Person alloc] initWithFirstName:@"龚" lastName:@"文" andAge:22];
Person *p8 = [[Person alloc] initWithFirstName:@"刘" lastName:@"双" andAge:49];
Person *p9 = [[Person alloc] initWithFirstName:@"邹" lastName:@"全" andAge:30];
Person *p10 = [[Person alloc] initWithFirstName:@"姚" lastName:@"天" andAge:25];
NSArray *la = [NSArray arrayWithObjects:p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, nil];
NSLog(@"未排序数组");
[la makeObjectsPerformSelector:@selector(reportState)];
//排序
NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
NSSortDescriptor *sd2 = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
NSSortDescriptor *sd3 = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
Exam_03_05_Sort(la, sd1, sd2, sd3);
Exam_03_05_Sort(la, sd2, sd1, sd3);
Exam_03_05_Sort(la, sd3, sd2, sd1);
//过滤
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"age > 30"];
NSArray *arraySubset = [la filteredArrayUsingPredicate: predicate];
NSLog(@"过滤后的数组");
[arraySubset makeObjectsPerformSelector:@selector(reportState)];
}
}
//字典
void Exam_03_10()
{
@autoreleasepool {
NSDictionary *dict1 = [[NSDictionary alloc] init];
NSArray *aValues = [NSArray arrayWithObjects:@"你好,中国", @"Hello, China!", nil];
NSArray *aKeys = [NSArray arrayWithObjects:@"ch", @"english", nil];
NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:aValues forKeys:aKeys];
NSLog(@"dict2 = %@", dict2);
NSLog(@"集合中有%lu个元素", dict2.count);
//遍历
for(NSString *s in [dict2 allValues])
{
NSLog(@"值: %@", s);
}
[dict2 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"键=%@, 值=%@", key, obj);
}];
}
}
//
// Exam_03_Impl.h
// Exam_03
//
// Created by yao_yu on 13-11-19.
// Copyright (c) 2013年 yao_yu. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property(strong)NSString *firstName;
@property(strong)NSString *lastName;
@property(assign) int age;
-(id)initWithFirstName:(NSString *)fName lastName:(NSString *)lName andAge:(int)a;
-(void)reportState;
@end
void Exam_03_01();
void Exam_03_02();
void Exam_03_05();
void Exam_03_10();
Exam_03_Impl.m
//
// Exam_03_Impl.m
// 使用对象集合
//
// Created by yao_yu on 13-11-19.
// Copyright (c) 2013年 yao_yu. All rights reserved.
//
#import "Exam_03_Impl.h"
void Exam_03_01()
{
@autoreleasepool {
NSArray *listOfLetters1 = [NSArray arrayWithObjects:@"A", @"B", @"C", nil];
NSLog(@"listOfLetters1 = %@", listOfLetters1);
NSNumber *n1 = [NSNumber numberWithInt: 1];
NSNumber *n2 = [NSNumber numberWithInt: 2];
NSNumber *n3 = [NSNumber numberWithInt: 3];
NSMutableArray *list2 = [[NSMutableArray alloc] initWithObjects: n1, n2, n3, nil];
NSLog(@"list2 = %@", list2);
id list[3];
list[0] = @"D";
list[1] = @"E";
list[2] = @"F";
NSMutableArray *list3 = [[NSMutableArray alloc] initWithObjects:list count:3];
NSLog(@"list3 = %@", list3);
}
}
void Exam_03_02()
{
@autoreleasepool {
NSMutableArray *ma = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString: @"A"],
[NSMutableString stringWithString: @"B"],
[NSMutableString stringWithString: @"C"],
[NSMutableString stringWithString: @"D"],
nil];
NSString *s1 = [ma objectAtIndex: 0];
NSLog(@"s1 = %@", s1);
NSString *s2 = [ma objectAtIndex: 1];
NSLog(@"S2 = %@", s2);
NSUInteger p = [ma indexOfObject: @"B"];
NSLog(@"位置 = %lu", p);
NSLog(@"数组元素个数 = %lu", ma.count);
for(NSMutableString *s in ma)
{
NSLog(@"字符串小写是 %@", [s lowercaseString]);
}
[ma makeObjectsPerformSelector:@selector(appendString:) withObject:@"-MORE"];
[ma enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"对象(%lu)的描述为%@", idx, [obj description]);
}];
}
}
@implementation Person
@synthesize firstName, lastName, age;
-(id)initWithFirstName:(NSString *)fName lastName:(NSString *)lName andAge:(int)a
{
self = [super init];
if(self){
self.firstName = fName;
self.lastName = lName;
self.age = a;
}
return self;
}
-(void)reportState
{
NSLog(@"我叫%@%@, 今年%i岁", self.firstName, self.lastName, self.age);
}
@end
void Exam_03_05_Sort(NSArray *la, NSSortDescriptor *sd1, NSSortDescriptor *sd2, NSSortDescriptor *sd3)
{
NSLog(@"按[%@,%@,%@]排序后的数组", [sd1 key], [sd2 key], [sd3 key]);
NSArray *sda1 = [NSArray arrayWithObjects:sd1, sd2, sd3, nil];
NSArray *sortedArray1 = [la sortedArrayUsingDescriptors: sda1];
[sortedArray1 makeObjectsPerformSelector:@selector(reportState)];
}
//数组排序,过滤
void Exam_03_05()
{
@autoreleasepool {
Person *p1 = [[Person alloc] initWithFirstName:@"张" lastName:@"三" andAge:33];
Person *p2 = [[Person alloc] initWithFirstName:@"李" lastName:@"四" andAge:24];
Person *p3 = [[Person alloc] initWithFirstName:@"王" lastName:@"五" andAge:45];
Person *p4 = [[Person alloc] initWithFirstName:@"赵" lastName:@"大" andAge:35];
Person *p5 = [[Person alloc] initWithFirstName:@"钱" lastName:@"小" andAge:32];
Person *p6 = [[Person alloc] initWithFirstName:@"孙" lastName:@"武" andAge:21];
Person *p7 = [[Person alloc] initWithFirstName:@"龚" lastName:@"文" andAge:22];
Person *p8 = [[Person alloc] initWithFirstName:@"刘" lastName:@"双" andAge:49];
Person *p9 = [[Person alloc] initWithFirstName:@"邹" lastName:@"全" andAge:30];
Person *p10 = [[Person alloc] initWithFirstName:@"姚" lastName:@"天" andAge:25];
NSArray *la = [NSArray arrayWithObjects:p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, nil];
NSLog(@"未排序数组");
[la makeObjectsPerformSelector:@selector(reportState)];
//排序
NSSortDescriptor *sd1 = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
NSSortDescriptor *sd2 = [NSSortDescriptor sortDescriptorWithKey:@"firstName" ascending:YES];
NSSortDescriptor *sd3 = [NSSortDescriptor sortDescriptorWithKey:@"lastName" ascending:YES];
Exam_03_05_Sort(la, sd1, sd2, sd3);
Exam_03_05_Sort(la, sd2, sd1, sd3);
Exam_03_05_Sort(la, sd3, sd2, sd1);
//过滤
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"age > 30"];
NSArray *arraySubset = [la filteredArrayUsingPredicate: predicate];
NSLog(@"过滤后的数组");
[arraySubset makeObjectsPerformSelector:@selector(reportState)];
}
}
//字典
void Exam_03_10()
{
@autoreleasepool {
NSDictionary *dict1 = [[NSDictionary alloc] init];
NSArray *aValues = [NSArray arrayWithObjects:@"你好,中国", @"Hello, China!", nil];
NSArray *aKeys = [NSArray arrayWithObjects:@"ch", @"english", nil];
NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:aValues forKeys:aKeys];
NSLog(@"dict2 = %@", dict2);
NSLog(@"集合中有%lu个元素", dict2.count);
//遍历
for(NSString *s in [dict2 allValues])
{
NSLog(@"值: %@", s);
}
[dict2 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"键=%@, 值=%@", key, obj);
}];
}
}
相关文章推荐
- Objective-C如何使用对象集合学习系列之一
- 63、实例 使用jQuery全局函数遍历数组和集合对象
- SharedObject使用:在FluorineFx.net与Flex中使用共享对象维护在线用户列表实例【转】
- JAVA学习第三十四课 (经常使用对象API)—List集合及其子类特点
- Objective-C如何使用对象集合之学习系列三
- 使用Object.definePropery方法定义一个只读对象实例
- JAVA基础学习之final关键字、遍历集合、日期类对象的使用、Math类对象的使用、Runtime类对象的使用、时间对象Date(两个日期相减)(5)
- Android动画学习(三)之使用ValueAnimator和ObjectAnimator实现动画实例
- OpenGL学习脚印:缓冲对象相关函数的使用(buffer object function)
- 对象转换为Map集合,使用JSONObject实现
- Object 对象方法学习之(1)—— 使用 Object.assign 复制对象、合并对象
- Java学习--List集合对象的使用注意事项
- OpenGL学习脚印:缓冲对象相关函数的使用(buffer object function usage)
- 初学Python的学习笔记9----面向对象编程(获取对象信息、实例属性和类属性)、面向对象高级编程(使用__slots__、使用@property)
- OC学习之道:OC对象,字典,集合的使用方法NSDictionary,NSMutableDictionary, NSSet,NSMutableSet,NSCountedSet
- OpenGL学习脚印:缓冲对象相关函数的使用(buffer object function usage)
- Android动画学习(三)之使用ValueAnimator和ObjectAnimator实现动画实例
- 用一个实例学习VBS中使用fso对象的基本方法
- JAVA学习第三十六课(经常使用对象API)— Set集合:HashSet集合演示
- json对象、json格式字符串、java对象【使用此net.sf.json.JSONObject将java对象[比如数组集合Map、Map扩展]转换成json格式字符串】