ObjectC----几个小练习

  //  Created By 郭仔   2015年04月04日20:35:30   生气

推荐博客:http://coderperson.com/

一.将“⽂文艺⻘青年”改成“213⻘青年”。

将 整数123 转换为字符串“123”。
将 “i love you”单词⾸首字⺟母变⼤大写 “I love You”
截取字符串“20|http://www.baidu.com”中 “|” 前⾯面和后⾯面的字符串, 并输出。

// NSString * str = [[NSString alloc] initWithString:@"文艺青年"];
    NSString * str = @"文艺青年";
    NSLog(@"%@",str);
    NSString * str2 = [str stringByReplacingOccurrencesOfString:@"文艺" withString:@"123"];
    NSLog(@"%@",str2);
    
     //=================================================
    
    
    NSInteger  a = 122;
    NSString * str3 = [NSString stringWithFormat:@"%ld",a];
    NSLog(@"%@",str3);
     //=================================================
  
    NSString * str4 = @"i love you";
    NSString * str5 = [str4 capitalizedString]; //字符串中每个单词首字母变大写
    NSLog(@"%@",str5);
     //=================================================
    
    NSString * str6 = @"20|http://www.baidu.com";
    NSArray * arry = [str6 componentsSeparatedByString:@"|"];
    for (NSString *s in arry) {
        NSLog(@"%@",s);
    }

难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过难过

二.往可变数组⾥里存储 int、float、double、BOOL、NSRange等类型数
据 遍历数组
提⽰示:将标量转换为NSNumber或者NSValue才能存储

NSNumber * int_number = [NSNumber numberWithInt:10];
    NSMutableArray * mularr = [NSMutableArray arrayWithObjects:int_number, nil];
    NSNumber * double_number = [NSNumber numberWithDouble:23.5];
    [mularr addObject:double_number];
    NSNumber * bool_number = [NSNumber numberWithBool:YES];
    [mularr addObject:bool_number];
    NSRange  range = {1,5}; // 注意这里不能带*号
    NSValue * range_struct = [NSValue valueWithRange:range];
    [mularr addObject:range_struct];
   // NSLog(@"%@",range_struct);
    for (int i = 0;i < [mularr count];i ++)
    {
        NSLog(@"%@",mularr[i]);
       // NSLog(@"%@",[mularr objectAtIndex:i]);
    }
    // 若需要变回原来的类型则需要用intValue,doubleValue等

睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉睡觉

三.实现简单通讯录操作。
  睡觉

1、定义联系⼈人类Contact。实例变量:姓名、性别、电话号码、住址、分组名称。⽅方 法:初始化⽅方法(姓名、电话号码)、显⽰示联系⼈人信息
 

2、在main.m中定义可变数组,管理所有联系⼈人。可以添加新联系⼈人对象,如果姓名 或电话号码为空,打印添加失败。

3、获取某个分组下的所有联系⼈人。

4、根据电话号码搜索联系⼈人。

5、获取所有⼥女性联系⼈人

6、根据姓名删除联系⼈人

7、删除某个分组全部联系⼈人 

8、展⽰示通讯录中所有联系⼈人

9、选做:定义AddressBook类,封装上述功能。

奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗奋斗
===================================================================================

我是在定义了一个NSMutableArray分类,然后在其中实现了不同的代码块

方法类:奋斗

- (void)addContact:(Contact *)contact;
- (void)getContactsOfAnyOneOfGroub:(NSString *)groub;
- (void)searchContactWithPhone:(NSString *)phone;
- (void)deleteContactWithName:(NSString *)name;
- (void)deleteContactOfGroub:(NSString *)groub;
- (void)showAllContacts;

实现: 睡觉

<span style="font-size:18px;">- (void)addContact:(Contact *)contact
{
    
        if (([contact.name isEqualToString:@""]==1)||([contact.phone isEqualToString:@""]==1)) {
            NSLog(@"添加失败!");
        }
        else{
            [self addObject:contact];
        }
        
}
- (void)getContactsOfAnyOneOfGroub:(NSString *)groub
{
    for(Contact *contact in self)
    {
        if ([contact.groub compare:groub] == 0)
        NSLog(@"%@",contact);
    }
}
- (void)searchContactWithPhone:(NSString *)phone
{
    int temp = 0;
    for(Contact *contact in self)
    {
        if ([contact.phone compare:phone] == 0)
        {
            NSLog(@"%@",contact);
            temp = 1;
        }
    }
    if (temp == 0) {
        NSLog(@"Do not have this contact");
    }
}
- (void)deleteContactWithName:(NSString *)name
{
    for (int i = 0; i < [self count]; i ++) {
        if ([[self[i] name] compare:name] == 0) {
            [self removeObject:self[i]];
            break;
        }
    }

}
- (void)deleteContactOfGroub:(NSString *)groub
{
    for (int i = 0; i < [self count]; i ++) {
        if ([[self[i] groub] compare:groub]==0) {
            [self removeObject:self[i]];
            i --; // 关键:回溯
        }
    }
}
- (void)showAllContacts
{
    for(Contact *contact in self)
        NSLog(@"%@",contact);
}
</span>
当然别忘了:快哭了

- (NSString *)description
{
   
    NSString * str = [[NSString alloc]initWithFormat:@"%@ %@ %@",_name,_phone,_groub];
    return str;
}


羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕 羡慕=======================================================================================
四.给NSString添加初始化⽅方法 initWithDate: forFormat:。
第⼀一个参数是NSDate对象,第⼆二个参数是NSString对象,第⼆二个 参数⽤用来指定NSDateFormatter的format。

- (instancetype)initWithDate:(NSDate *)date forFormat:(NSString *)str
{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:str];
    str = [formatter stringFromDate:date];
    return str ;
}
// =====================================
    NSDate * date = [NSDate date];
    NSLog(@"dd");
    NSString * str = [[NSString alloc] initWithDate:date forFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSLog(@"%@",str);
// =====================================
五.给NSMutableArray添加convert⽅方法,实现数组倒序

+ (void)convert:(NSMutableArray *)mulArray
{
    for (int i = 0,j = (int)[mulArray count]-1; i<j; i ++,j --) {
        [mulArray exchangeObjectAtIndex:i withObjectAtIndex:j];
    }
    
}
- (void)convert:(NSMutableArray *)mulArray
{
    for (int i = 0,j = (int)[mulArray count]-1; i<j; i ++,j --) {
        [mulArray exchangeObjectAtIndex:i withObjectAtIndex:j];
    }

}
分别用类方法和对象方法实现,差别在哪里呢 羡慕你懂得。。。。

六.定义结婚协议,Woman是代理,为Boy做家务挣钱。。。。。

@protocol MarryProcotol <NSObject>

@required
- (void)makeMoney;

@optional
- (void)cook;

@end
Woman 得意

@interface Woman : NSObject<MarryProcotol>

- (void)makeMoney
{
    NSLog(@"Woman--makeMoney");
}

- (void)cook
{
    NSLog(@"Woman--cook");
}

Boy 生气

id<MarryProcotol> _protocol;

- (void)setProtocol:(id<MarryProcotol>)protocol;
- (id<MarryProcotol>)protocol;

- (void)playGame;
- (void)watchFilm;
- (void)playGame
{
    NSLog(@"Boy--playGame");
    [_protocol makeMoney];
}
- (void)watchFilm
{
    NSLog(@"Boy--watchFilm");
    [_protocol cook];
}

- (void)setProtocol:(id<MarryProcotol>)protocol
{
    _protocol = protocol;
}
- (id<MarryProcotol>)protocol
{
    return _protocol;
}

下面Woman要位Boy做家务挣钱了

安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静 安静
    Boy *boy =[[Boy alloc] init];
    Woman *woman = [[Woman alloc]init];
    [boy setProtocol:woman];
    [boy playGame];
    [boy watchFilm];
七.定义⼀一个block,返回值为BOOL,有两个NSString参数。实现:判
断字符串是否相等。

typedef BOOL (^Block)(NSString*,NSString*);
 Block block = ^(NSString *str1,NSString *str2){
        if ([str1 isEqualToString:str2]) {
            return YES;
        }
        else
            return NO;
    };
    if(block(@"sds",@"ddd"))
    {
        NSLog(@"Equal");
    }
    else
        NSLog(@"!Equal");

</pre>八.定义⼀一个block,返回值为NSInteger,有两个参数,⼀一个是 NSArray,⼀一个是NSString。实现:判断数组时候包含此字符串,如 果包含,返回字符串的下标,如果不包括,返回-1<pre code_snippet_id="636147" snippet_file_name="blog_20150404_18_5293409" name="code" class="objc">  __block int i = 0;
    Block2 block2 = ^(NSString *str,NSArray * arr){
        for (i = 0; i < [arr count]; i ++) {
            NSString * str2 = [arr objectAtIndex:i];
            if([str isEqualToString:str2])
                return (NSInteger)i;
        }
        return (NSInteger)-1;
    
    };
    NSArray * array = [NSArray arrayWithObjects:@"guo",@"zai", nil];
    NSLog(@"%ld",block2(@"zai",array));

敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打敲打

九。创建⼀一个数组,初始化为@“123",@"21",@"33",@"69", @“108”,
@“256”。使⽤用Block语法,进⾏行数组的排序。并输出内容。排序结果:108 123 21 256 33 69
!
提⽰示:sortedArrayWithOptions:usingComparator:
  NSArray * arr = [NSArray arrayWithObjects:@"123",@"21",@"33",@"69",@"108",@"256" ,nil];
    NSArray * arr2 = [arr sortedArrayWithOptions:NSSortStable usingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
        if((int)[obj1 compare: obj2]>0)
            return NSOrderedDescending;
        else if([obj1 compare: obj2]==0)
            return NSOrderedSame;
        else return NSOrderedAscending;
    }];
    
    for (NSString * s in arr2) {
        NSLog(@"%@",s);
    }


可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜可怜

推荐大家关注微信号:iOSWorld,或者扫面下面二维码关注,大家可以学到很多干货:









评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值