一.將“⽂文藝⻘青年”改成“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或者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><span style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">八.定義⼀一個block,返回值爲NSInteger,有兩個參數,⼀一個是 NSArray,⼀一個是NSString。實現:判斷數組時候包含此字符串,如 果包含,返回字符串的下標,如果不包括,返回-1</span><br style="color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;" /><pre code_snippet_id="636147" snippet_file_name="blog_20150404_18_5293409" name="code" class="objc" style="white-space: pre-wrap; word-wrap: break-word; color: rgb(85, 85, 85); font-size: 15px; line-height: 35px;"> __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));
@“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);
}