// 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);
}
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;
- (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);
}
当然别忘了:
- (NSString *)description
{
NSString * str = [[NSString alloc]initWithFormat:@"%@ %@ %@",_name,_phone,_groub];
return str;
}
- (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
分别用类方法和对象方法实现,差别在哪里呢 你懂得。。。。
六.定义结婚协议,Woman是代理,为Boy做家务挣钱。。。。。
@protocol MarryProcotol
@required
- (void)makeMoney;
@optional
- (void)cook;
@end
Woman
@interface Woman : NSObject
- (void)makeMoney
{
NSLog(@"Woman--makeMoney");
}
- (void)cook
{
NSLog(@"Woman--cook");
}
id _protocol;
- (void)setProtocol:(id)protocol;
- (id)protocol;
- (void)playGame;
- (void)watchFilm;
- (void)playGame
{
NSLog(@"Boy--playGame");
[_protocol makeMoney];
}
- (void)watchFilm
{
NSLog(@"Boy--watchFilm");
[_protocol cook];
}
- (void)setProtocol:(id)protocol
{
_protocol = protocol;
}
- (id)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");
八.定义⼀一个block,返回值为NSInteger,有两个参数,⼀一个是 NSArray,⼀一个是NSString。实现:判断数组时候包含此字符串,如 果包含,返回字符串的下标,如果不包括,返回-1__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));
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,或者扫面下面二维码关注,大家可以学到很多干货: