您的位置:首页 > 移动开发 > Objective-C

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);
}];
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  objective-c 实例
相关文章推荐