【GeekBand】Week2. 笔记整理

2016-03-14  本文已影响0人  喵小扣

1. 【OC/2-1】字符串处理

NSString *str = @"Hello World!";
NSString *str = [[NSString alloc]initWithCString:"Hello World!"
                                          encoding:NSUTF8StringEncoding];
NSString *str = [NSString stringWithCString:"Hello World!"
                                     encoding:NSUTF8StringEncoding];
NSMutableString *mustr = [NSMutableString stringWithCapacity:100];
str.length
[str length]
str1 isEqualToString: str2 //指针指向的【字符串】是否相等
str1 == str2 //【指针】是否指向同一对象,【引用】是否相等
[str characterAtIndex: i]
str = str.uppercaseString; //大写
str = str.lowercaseString; //小写
str = str.capitalizedString; //首字母大写
/**********
typedef struct _NSRange {  
      unsigned int location; //范围起始位置
      unsigned int length; //范围内所含元素的个数
}NSRange;
**********/
if ([str1 rangeOfString:str].location != NSNotFound) {  
      //找到了
}
/*
NSRange range = [str1 rangeOfString:@"Hello"];
range.location
range.length
*/
substringToIndex //从第一个字符串开始截取,到指定长度位置,但是不包括该指定位置位置字符串
substringFromIndex //从指定位置开始截取字符串到最后,所截取位置包含该指定位置
substringWithRange //传入NSRange类型,表示从哪里开始截取和长度,返回字符串类型。
hasPrefix
NSString* format = [NSString stringWithFormat: @"[%d, %d]", 100, 200];
[mustr3 appendString:@"Hello Objective"];
[mustr3 insertString:@"-C" atIndex:mustr3.length];
[mustr3 setString:@"Hi Objective"];
NSRange replaceRange = NSMakeRange(0, 2);
[mustr3 replaceCharactersInRange:replaceRange withString:@"Hello"];
NSRange deleteRange = NSMakeRange(5, 10);
[mustr3 deleteCharactersInRange:deleteRange];

2.【OC/2-2】集合类型

NSArray & NSMutableArray

NSArray *array = [NSArray arrayWithObjects:@"Shanghai",@"Beijing", nil];
NSArray *array = [[NSArray alloc] initWithObjects:@"Shanghai",@"Beijing", nil];
NSArray *array = @[@"Shanghai",@"Beijing"];
NSMutableArray *muArray=[NSMutableArray arrayWithObjects:p1,p2,p3,p4, nil];
//nil为空
array.count
NSLog(@"array: %@" ,array);
/*不能直接放基本数值类型,使用NSNumber将NSInteger包装为对象*/
NSNumber *numberObject1=[NSNumber numberWithInteger:300];
NSNumber *numberObject2=@300u;//使用字面常量初始化
/*使用NSValue将struct包装为对象*/
Point point;
point.h=100;
point.v=200;
NSValue *pointObject=[NSValue value:&point withObjCType:@encode(Point)];
/*不能存放nil*/
NSNull* nullValue=[NSNull null];
for (类 对象 in array)
-(NSUInteger)indexOfObject:(id)anObject; //查找对象(isEqual)
-(NSUInteger)indexOfObjectIdenticalTo:(id)anObject; //查找指针
NSArray* sortArray = [array sortedArrayUsingSelector:@selector(compare:)];
[muArray addObject:p];
[muArray removeObjectAtIndex:2];
[muArray insertObject:p atIndex:1];
muArray1[0]=p;

NSSet & NSMutableSet

特点:无序集合,对象唯一,hash实现查找更快

NSDictionary & NSMutableDictionary

key-value对

NSDictionary *dictionary1 = @{
      @"Shanghai" : p1,
      @"Beijing" : p2,
      @"New York" : p3,
      @"Paris" : p4
};
NSMutableDictionary *dictionary2 = 
[NSMutableDictionary dictionaryWithObjectsAndKeys:
                                p1, @"Shanghai",
                                p2, @"Beijing",
                                p3, @"New York",
                                p4, @"Paris",
                                nil];
[dictionary1 objectForKey:@"Beijing"]
dictionary1[@"Shanghai"]
for(NSString* key in dictionary1) {
      id object=dictionary1[key];
      NSLog(@"key:%@, object:%@", key, object);
}
[dictionary2 setObject:p5 forKey:@"London"];
[dictionary2 removeObjectForKey:@"Shanghai"];

3. 【OC/2-3】ARC

自动释放池 @autoreleasepool

4. 【OC/2-4】类型合同:协议

@protocol

if ([obj conformsToProtocol:@protocol(AProtocol)]) {
      //遵守了协议
}

5. 【OC/2-5】类别与扩展

类别

扩展

上一篇下一篇

猜你喜欢

热点阅读