OC 字典 存储联系人信息 求大神优化代码

1、定义联系?人类Contact。实例变量:姓名(拼?音,?首字?母?大写)、性别、电话号码、 住址、分组名称、年龄。?方法:?自定义初始化?方法(姓名、电话号码)、显?示联系?人信息?

2、在main.m中定义字典,分组管理所有联系?人。分组名为26个?大写的英?文字?母。

? 3、可以添加联系?人对象,如果姓名或电话号码为空,添加失败。添加联系?人到匹配的分 组。?

4、获取某个分组名称下所有联系?人,并且按照姓名升序排列。?

5、从通讯录中根据电话号码搜索联系?人。?

6、获取所有?女性的联系?人,并且按照年龄的降序排列。?

7、根据姓名删除某个联系?人。? 8、删除某个分组的全部联系?人。

.H文件

Contact.h

#import <Foundation/Foundation.h>

@interface Contact : NSObject

@property (nonatomic, retain)NSString *name;

@property (nonatomic, retain)NSString *sex;

@property (nonatomic, assign)NSInteger age;

@property (nonatomic, retain)NSString *addrs;

@property (nonatomic, retain)NSString *phone;

@property (nonatomic, retain)NSString *group;

- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age adds:(NSString *)adds phone:(NSString *)phone group:(NSString *)group;

@end

实现文件.M

#import "Contact.h"

@implementation Contact

- (instancetype)initWithName:(NSString *)name sex:(NSString *)sex age:(NSInteger)age adds:(NSString *)adds phone:(NSString *)phone group:(NSString *)group

{

self = [self init];

if (self) {

[self setName:name];

[self setSex:sex];

[self setAge:age];

[self setAddrs:adds];

[self setPhone:phone];

[self setGroup:group];

}

return self;

}

- (NSString *)description

{

return [NSString stringWithFormat:@"name:%@, phone:%@, group:%@", self.name, self.phone, self.group];

}

@end

MAIN () 函数

// 1、定义联系?类Contact。实例变量:姓名(拼音,?字母大写)、性别、电话号码、di址、分组名称、年龄。方法:?定义初始化方法(姓名、电话号码)、显?示联系人信息

// 2.在main.m中定义字典,分组管理所有联系人。分组名为26个大写的英?字母。

// 定义数组 存储联系人对象

NSMutableArray *arr0 = [NSMutableArray array];

NSMutableArray *arr1 = [NSMutableArray array];

NSMutableArray *arr2 = [NSMutableArray array];

NSMutableArray *arr3 = [NSMutableArray array];

NSMutableArray *arr4 = [NSMutableArray array];

NSMutableArray *arr5 = [NSMutableArray array];

NSMutableArray *arr6 = [NSMutableArray array];

NSMutableArray *arr7 = [NSMutableArray array];

NSMutableArray *arr8 = [NSMutableArray array];

NSMutableArray *arr9 = [NSMutableArray array];

NSMutableArray *arr10 = [NSMutableArray array];

NSMutableArray *arr11 = [NSMutableArray array];

NSMutableArray *arr12 = [NSMutableArray array];

NSMutableArray *arr13 = [NSMutableArray array];

NSMutableArray *arr14 = [NSMutableArray array];

NSMutableArray *arr15 = [NSMutableArray array];

NSMutableArray *arr16 = [NSMutableArray array];

NSMutableArray *arr17 = [NSMutableArray array];

NSMutableArray *arr18 = [NSMutableArray array];

NSMutableArray *arr19 = [NSMutableArray array];

NSMutableArray *arr20 = [NSMutableArray array];

NSMutableArray *arr21 = [NSMutableArray array];

NSMutableArray *arr22 = [NSMutableArray array];

NSMutableArray *arr23 = [NSMutableArray array];

NSMutableArray *arr24 = [NSMutableArray array];

NSMutableArray *arr25 = [NSMutableArray array];

NSMutableArray *arr26 = [NSMutableArray array];

NSMutableDictionary *mDic = [NSMutableDictionary dictionary];

[mDic setObject:arr1 forKey:@"A"];

[mDic setObject:arr2 forKey:@"B"];

[mDic setObject:arr3 forKey:@"C"];

[mDic setObject:arr4 forKey:@"D"];

[mDic setObject:arr5 forKey:@"E"];

[mDic setObject:arr6 forKey:@"F"];

[mDic setObject:arr7 forKey:@"G"];

[mDic setObject:arr8 forKey:@"H"];

[mDic setObject:arr9 forKey:@"I"];

[mDic setObject:arr10 forKey:@"J"];

[mDic setObject:arr11 forKey:@"K"];

[mDic setObject:arr12 forKey:@"L"];

[mDic setObject:arr13 forKey:@"M"];

[mDic setObject:arr14 forKey:@"N"];

[mDic setObject:arr15 forKey:@"O"];

[mDic setObject:arr16 forKey:@"P"];

[mDic setObject:arr17 forKey:@"Q"];

[mDic setObject:arr18 forKey:@"R"];

[mDic setObject:arr19 forKey:@"S"];

[mDic setObject:arr20 forKey:@"T"];

[mDic setObject:arr21 forKey:@"U"];

[mDic setObject:arr22 forKey:@"V"];

[mDic setObject:arr23 forKey:@"W"];

[mDic setObject:arr24 forKey:@"X"];

[mDic setObject:arr25 forKey:@"Y"];

[mDic setObject:arr26 forKey:@"Z"];

Contact *p1 = [[Contact alloc] initWithName:@"Arwwr" sex:@"m" age:22 adds:@"a" phone:@"188" group:@"student"];

Contact *p2 = [[Contact alloc] initWithName:@"B" sex:@"f" age:22 adds:@"a" phone:@"188" group:@"student"];

Contact *p3 = [[Contact alloc] initWithName:@"C" sex:@"m" age:22 adds:@"a" phone:@"188" group:@"student"];

Contact *p4 = [[Contact alloc] initWithName:@"D" sex:@"f" age:22 adds:@"a" phone:@"188" group:@"student"];

Contact *p5 = [[Contact alloc] initWithName:@"E" sex:@"m" age:22 adds:@"a" phone:@"177" group:@"student"];

Contact *p6 = [[Contact alloc] initWithName:@"F" sex:@"f" age:22 adds:@"a" phone:@"177" group:@"student"];

Contact *p7 = [[Contact alloc] initWithName:@"G" sex:@"m" age:22 adds:@"a" phone:@"177" group:@"student"];

Contact *p8 = [[Contact alloc] initWithName:@"H" sex:@"f" age:22 adds:@"a" phone:@"177" group:@"student"];

Contact *p9 = [[Contact alloc] initWithName:@"I" sex:@"m" age:22 adds:@"a" phone:@"166" group:@"student"];

Contact *p10 = [[Contact alloc] initWithName:@"J" sex:@"f" age:22 adds:@"a" phone:@"166" group:@"student"];

Contact *p11 = [[Contact alloc] initWithName:@"K" sex:@"m" age:22 adds:@"a" phone:@"166" group:@"student"];

Contact *p12 = [[Contact alloc] initWithName:@"L" sex:@"m" age:22 adds:@"a" phone:@"166" group:@"student"];

Contact *p13 = [[Contact alloc] initWithName:@"Aoillhgf" sex:@"f" age:22 adds:@"a" phone:@"155" group:@"student"];

Contact *p14 = [[Contact alloc] initWithName:@"Aheokjhht" sex:@"m" age:22 adds:@"a" phone:@"155" group:@"student"];

Contact *p15 = [[Contact alloc] initWithName:@"Acvnutrh" sex:@"f" age:22 adds:@"a" phone:@"155" group:@"student"];

Contact *p16 = [[Contact alloc] initWithName:@"Ayuid" sex:@"m" age:22 adds:@"a" phone:@"155" group:@"student"];

Contact *p17 = [[Contact alloc] initWithName:@"Ayuidfgh" sex:@"f" age:22 adds:@"a" phone:@"144" group:@"student"];

Contact *p18 = [[Contact alloc] initWithName:@"R" sex:@"m" age:22 adds:@"a" phone:@"144" group:@"student"];

Contact *p19 = [[Contact alloc] initWithName:@"S" sex:@"f" age:22 adds:@"a" phone:@"144" group:@"student"];

Contact *p20 = [[Contact alloc] initWithName:@"T" sex:@"m" age:22 adds:@"a" phone:@"144" group:@"student"];

Contact *p21 = [[Contact alloc] initWithName:@"U" sex:@"f" age:22 adds:@"a" phone:@"133" group:@"student"];

Contact *p22 = [[Contact alloc] initWithName:@"V" sex:@"m" age:22 adds:@"a" phone:@"133" group:@"student"];

Contact *p23 = [[Contact alloc] initWithName:@"W" sex:@"f" age:22 adds:@"a" phone:@"133" group:@"student"];

Contact *p24 = [[Contact alloc] initWithName:@"X" sex:@"m" age:22 adds:@"a" phone:@"133" group:@"student"];

Contact *p25 = [[Contact alloc] initWithName:@"Y" sex:@"f" age:22 adds:@"a" phone:@"122" group:@"student"];

Contact *p26 = [[Contact alloc] initWithName:@"Z" sex:@"f" age:22 adds:@"a" phone:@"122" group:@"student"];

Contact *p27 = [[Contact alloc] initWithName:@"ABwerwe" sex:@"m" age:22 adds:@"a" phone:@"122" group:@"student"];

Contact *p28 = [[Contact alloc] initWithName:@"BA" sex:@"m" age:22 adds:@"a" phone:nil group:@"student"];

[arr0 addObject:p1];

[arr0 addObject:p2];

[arr0 addObject:p3];

[arr0 addObject:p4];

[arr0 addObject:p5];

[arr0 addObject:p6];

[arr0 addObject:p7];

[arr0 addObject:p8];

[arr0 addObject:p9];

[arr0 addObject:p10];

[arr0 addObject:p11];

[arr0 addObject:p12];

[arr0 addObject:p13];

[arr0 addObject:p14];

[arr0 addObject:p15];

[arr0 addObject:p16];

[arr0 addObject:p17];

[arr0 addObject:p18];

[arr0 addObject:p19];

[arr0 addObject:p20];

[arr0 addObject:p21];

[arr0 addObject:p22];

[arr0 addObject:p23];

[arr0 addObject:p24];

[arr0 addObject:p25];

[arr0 addObject:p26];

[arr0 addObject:p27];

[arr0 addObject:p28];

// 3、可以添加联系人对象,如果姓名或电话号码为空,添加失败。添加联系人到匹配的分组。

for (NSInteger i = 0; i < 28; i++) {

Contact *p = [arr0 objectAtIndex:i];

if (p.phone == nil || p.name == nil) {

NSLog(@"name: %@, phone : %@  姓名或者号码为空,添加失败", p.name, p.phone);

}else{

[arr0 addObject:p];

}

}

for (NSInteger i = 0; i < 28; i++) {

Contact *p = [arr0 objectAtIndex:i];

if([p.name hasPrefix:@"A"]){

[arr1 addObject:p];

}else if([p.name hasPrefix:@"B"]){

[arr2 addObject:p];

}else if([p.name hasPrefix:@"C"]){

[arr3 addObject:p];

}else if([p.name hasPrefix:@"D"]){

[arr4 addObject:p];

}else if([p.name hasPrefix:@"E"]){

[arr5 addObject:p];

}else if([p.name hasPrefix:@"F"]){

[arr6 addObject:p];

}else if([p.name hasPrefix:@"G"]){

[arr7 addObject:p];

}else if([p.name hasPrefix:@"H"]){

[arr8 addObject:p];

}else if([p.name hasPrefix:@"I"]){

[arr9 addObject:p];

}else if([p.name hasPrefix:@"J"]){

[arr10 addObject:p];

}else if([p.name hasPrefix:@"K"]){

[arr11 addObject:p];

}else if([p.name hasPrefix:@"L"]){

[arr12 addObject:p];

}else if([p.name hasPrefix:@"M"]){

[arr13 addObject:p];

}else if([p.name hasPrefix:@"N"]){

[arr14 addObject:p];

}else if([p.name hasPrefix:@"O"]){

[arr15 addObject:p];

}else if([p.name hasPrefix:@"P"]){

[arr16 addObject:p];

}else if([p.name hasPrefix:@"Q"]){

[arr17 addObject:p];

}else if([p.name hasPrefix:@"R"]){

[arr18 addObject:p];

}else if([p.name hasPrefix:@"S"]){

[arr19 addObject:p];

}else if([p.name hasPrefix:@"T"]){

[arr20 addObject:p];

}else if([p.name hasPrefix:@"U"]){

[arr21 addObject:p];

}else if([p.name hasPrefix:@"V"]){

[arr22 addObject:p];

}else if([p.name hasPrefix:@"W"]){

[arr23 addObject:p];

}else if([p.name hasPrefix:@"X"]){

[arr24 addObject:p];

}else if([p.name hasPrefix:@"Y"]){

[arr25 addObject:p];

}else if([p.name hasPrefix:@"Z"]){

[arr26 addObject:p];

}

}

for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

// 获取每一个Key

NSString *key = [mDic.allKeys objectAtIndex:i];

// 根据key获取对应value

NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

}

//    4、获取某个分组名称下所有联系人,并且按照姓名升序排列。

NSLog(@"23453");

NSMutableArray *cName = [mDic objectForKey:@"A"];

for (NSInteger i = 0; i < cName.count - 1; i++) {

for (NSInteger j = 0; j < cName.count - 1 - i; j++) {

NSComparisonResult result = [[[cName objectAtIndex:j] name] compare:[[cName objectAtIndex:j + 1] name] ];

if (result == NSOrderedDescending) {

// 按照 a b c 排序

[cName exchangeObjectAtIndex:j withObjectAtIndex:j + 1];

}

}

//NSLog(@"%@", [cName objectAtIndex:i]);

// NSLog(@"%@",[[cName objectAtIndex:i] name]);

}

for (NSInteger i = 0; i < cName.count ; i++) {

NSLog(@"%@", [cName objectAtIndex:i]);

NSLog(@"%@",[[cName objectAtIndex:i] name]);

}

//    5、从通讯录中根据电话号码搜索联系人。

for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

// 获取每一个Key

NSString *key = [mDic.allKeys objectAtIndex:i];

// 根据key获取对应value

//NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

NSArray *t = [mDic objectForKey:key];

for (NSInteger i = 0; i < t.count; i++){

Contact *person = [t objectAtIndex:i];

if ([[person phone] isEqualToString:@"188"] ) {

NSLog(@"name %@,phone %@", person.name, person.phone);

}

}

}

//    for (NSString *p in mDic) {

//        NSString *key = [mDic objectForKey:p];

//        NSArray *t = [mDic objectForKey:@"A"];

//        // NSLog(@"%@ %@", [[t objectAtIndex:2] name], [[t objectAtIndex:2] phone]);

//        for (NSInteger i = 0; i < t.count; i++){

//            Contact *person = [t objectAtIndex:i];

//            if ([[person phone] isEqualToString:@"188"] ) {

//                NSLog(@"%@, %@", person.name, person.phone);

//            }

//        }

//

//    }

//    6、获取所有女性的联系人,并且按照年龄的降序排列。

for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

// 获取每一个Key

NSString *key = [mDic.allKeys objectAtIndex:i];

// 根据key获取对应value

//NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

NSMutableArray *t = [mDic objectForKey:key];

for (NSInteger i = 0; i < t.count; i++){

Contact *person = [t objectAtIndex:i];

if ([[person sex] isEqualToString:@"f"] ) {

NSLog(@"name %@,sex %@", person.name, person.sex);

}

}

}

//    7、根据姓名删除某个联系?。

for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

// 获取每一个Key

NSString *key = [mDic.allKeys objectAtIndex:i];

// 根据key获取对应value

//NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

NSMutableArray *t = [mDic objectForKey:key];

for (NSInteger i = 0; i < t.count; i++){

Contact *person = [t objectAtIndex:i];

if ([[person name] isEqualToString:@"T"] ) {

NSLog(@"name %@,sex %@", person.name, person.sex);

[t removeObjectAtIndex:i];

}

}

}

//

BOOL r = YES;

for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

// 获取每一个Key

NSString *key = [mDic.allKeys objectAtIndex:i];

// 根据key获取对应value

//NSLog(@"key: %@, value: %@", key, [mDic objectForKey:key]);

NSMutableArray *t = [mDic objectForKey:key];

if (r) {

for (NSInteger i = 0; i < t.count; i++){

Contact *person = [t objectAtIndex:i];

if ([[person name] isEqualToString:@"T"] ) {

NSLog(@"name %@,sex %@", person.name, person.sex);

}else {

NSLog(@"没有 T 人 ");

r = NO;

}

}

}

}

//    8、删除某个分组的全部联系人。

for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

// 获取每一个Key

NSString *key = [mDic.allKeys objectAtIndex:i];

// 删除

if ([key isEqualToString:@"B"]) {

[mDic removeObjectForKey:key];

}

NSLog(@"88888%@", mDic);

}

// 打印

for (NSInteger i = 0; i < mDic.allKeys.count; i++) {

// 获取每一个Key

//NSString *key = [mDic.allKeys objectAtIndex:i];

NSLog(@"88888%@", mDic);

}

return 0;

}

时间: 2024-10-08 02:08:17

OC 字典 存储联系人信息 求大神优化代码的相关文章

求大神 &nbsp; 谁做过类似淘宝的sku商品信息组合

求大神   谁做过类似淘宝的sku商品信息组合

python 抓取搜狗微信出现的问题,求大神解决

爬取到的data不是想要获取文章页面的源码,而是跳转到验证码的页面的源码.我网上查了一下是由于访问过于频繁导致的,我也加了time.sleep和改了请求头但还是显示不了,求大神支招,除了识别验证码的方式还能怎么做?? import re import urllib.request import time import urllib.error headers = {'User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/2

ssh2整合: No bean named &#39;sessionFactory&#39; is defined(求大神指点)

applicationContext.xml 中已经配置 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="hibernate

开发的一个android程序,总是显示无法运行,就退出了。但是没有报错。求大神解救啊!

============问题描述============ 代码如下: 在网上查有人说是intent的问题 原来没有加入监听器的时候没有问题 我是个新手,问题有点没水平啦~ 求大神啊.谢谢谢谢 ============解决方案1============ 把log贴上来看看,是不是menu这个activity没有在manifest 声明 ============解决方案2============ setonclicklistener那句话,把new helplistener前面的那个类型转换删除 =

用java制作简单登陆窗口,求大神教导

import java.awt.Container; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import java

hdu 1008为何不对?求大神指导!

/*#include<iostream> using namespace std; int main(){ int n; while (cin >> n){ if (n == 0) break; else if (n > 0 && n < 100){ int sum=0, max,min; int *f = new int[n]; for (int i = 0; i < n; i++) cin >> f[i]; max = min =

跪求大神指教一个ImageButton与surfaceView问题

============问题描述============ 用xml文件写好了imagebutton按钮,然后让surfaceview连接到写好了的imagebutton按钮的ml文件当中,怎么实现点击Imagebutton消除用surfaceview中代码写得运动的物体.跪求大神指教... ============解决方案1============ 你可以试试自己定义接口回调,或者通过广播来处理.

安卓摄像头对焦问题,求大神支招。

============问题描述============ 查了一些资料,都没有找到,手机摄像头对选定的区域对焦的方法,就像小米照相机一样,点击哪.哪里显示就特别清晰的那个功能,求大神 支招 怎么做到这个功能.... ============解决方案1============ 对焦这个问题,早期版本未能解决.安卓4.0新增的Camera类,有一个setFocusAreas方法可以设置焦距. 早起SDK因为手机品种不齐,有些缺乏硬件支持,所以没有一个统一的对焦接口,新的SDK里已经出现了. ====

第一个简单的flask程序(有问题,求大神帮忙解决)

1.初始化 1 2 3 4 5 6 7 8 9 10 11 12 13 from flask import Flask app = Flask(__name__) @app.route('/') def index():  return '<h1>Hello World!</h1>' if __name__ == '__main__':  app.run(debug=True) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2