----------------------
ASP.Net+Unity开发、
.Net培训、期待与您交流! ----------------------
NSNumber
----------------------
ASP.Net+Unity开发、
.Net培训、期待与您交流! ----------------------
NSMutableString
NSString是不可变的,不能删除字符或者添加字符。NSString有一个子类NSMutableString,称为”可变字符串”
创建可变字符串的常用方法
-(id)initWithCapacity:(NSUInteger)capacity
+ (id)stringWithCapacity:(NSUInteger)capacity
capacity只是一个最优值,字符串的大小并不仅限于所提供的容量,设置了capacity,可以预分配一块内存来存储它,操作速度会快很多
当然,也可以使用创建NSString的方法来创建NSMutableString,因为NSMutableString是NSString的子类,NSString能用的方法,NSMutableString都能使用
NSMutableString常用处理
-(void)setString:(NSString *)aString
初始化完毕后可以使用此方法设置字符串的内容
- (void)appendString:(NSString*)aString
-(void)appendFormat:(NSString *)format, ...
这两个方法都是在尾部拼接一段字符串
-(void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString
将range位置的字符串替换为aString
-(void)insertString:(NSString *)aString atIndex:(NSUInteger)loc
在loc这个位置插入aString(aString的起始位置是就loc)
-(void)deleteCharactersInRange:(NSRange)range
删除range这个范围的字符串(经常跟rangeOfString:一起使用删除特定的字符串)
NSNumber
NSNumber可以将基本数据类型包装成对象,这样就可以间接将基本数据类型存进NSArray、NSDictionary等集合中
2.1常见的初始化方法:
+ (NSNumber *)numberWithChar:(char)value
+ (NSNumber *)numberWithInt:(int)value
+ (NSNumber *)numberWithFloat:(float)value
+ (NSNumber *)numberWithBool:(BOOL)value
- (id)initWithChar:(char)value
- (id)initWithInt:(int)value
- (id)initWithFloat:(float)value
- (id)initWithBool:(BOOL)value
2.2NSNumber常用方法
- (char)charValue
- (int)intValue
- (double)doubleValue
- (BOOL)boolValue
- (NSString *)stringValue
- (NSComparisonResult)compare:(NSNumber *)otherNumber
- (BOOL)isEqualToNumber:(NSNumber *)number
NSArray
用来存储对象的有序列表,它是不可变的
不能直接存储C语言中的基本数据类型,如int、float、enum、struct,也不能存储nil
创建NSArray的常用方法
+ (id)array
+ (id)arrayWithObject:(id)anObject
+ (id)arrayWithObjects:(id)firstObj, ...
+ (id)arrayWithArray:(NSArray *)array
- (id)initWithObjects:(id)firstObj, ...
- (id)initWithArray:(NSArray *)array
+ (id)arrayWithContentsOfFile:(NSString *)path
+ (id)arrayWithContentsOfURL:(NSURL *)url
- (id)initWithContentsOfFile:(NSString *)path
- (id)initWithContentsOfURL:(NSURL *)url
3.1NSArray查询
-(NSUInteger)count
获取集合元素个数
- (BOOL)containsObject:(id)anObject
是否包含某一个元素
-(id)lastObject
返回最后一个元素
-(id)objectAtIndex:(NSUInteger)index
获得index位置对象的元素
-(NSUInteger)indexOfObject:(id)anObject
查找元素的位置
-(NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range
在range范围内查找元素的位置
3.2NSArray比较
-(BOOL)isEqualToArray:(NSArray *)otherArray
比较两个集合内容是否相同
-(id)firstObjectCommonWithArray:(NSArray *)otherArray
返回两个集合中第一个相同的对象元素
3.3NSArray给元素发送消息
- (void)makeObjectsPerformSelector:(SEL)aSelector
- (void)makeObjectsPerformSelector:(SEL)aSelector withObject:(id)argument
让集合里面的所有元素都执行aSelector这个方法
3.4NSArray遍历
普通遍历
NSUInteger count = [array count];
for (int i = 0; i < count; i++) {
id obj = [array objectAtIndex:i];
}
快速遍历
for (id obj in array)
block遍历
[array enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
NSLog(@"%@ - %zi", object, index);
}]
NSMutableArray
可变的NSArray,NSArray的子类,可以随意的添加或者删除元素
创建NSMutableArray的方法:
+ (id)arrayWithCapacity:(NSUInteger)numItems
- (id)initWithCapacity:(NSUInteger)numItems
也可以使用创建NSArray的方法来创建NSMutableArray
当一个元素被加到集合中时,会执行一次retain操作;当一个元素从集合中移除时,会执行一次release操作;当集合被销毁时(调用了dealloc),集合里的所有元素都会执行一次release操作(这个原则还适用于其他集合:NSDictionary\NSSet等)
4.1NSMutableArray添加元素
-(void)setArray:(NSArray *)otherArray
设置集合元素
- (void)addObject:(id)anObject
添加一个元素
-(void)addObjectsFromArray:(NSArray *)otherArray
添加otherArray的全部元素到集合中
-(void)insertObject:(id)anObject atIndex:(NSUInteger)index
在index位置插入一个元素
-(void)insertObjects:(NSArray *)objects atIndexes:(NSIndexSet *)indexes
在indexes指定的位置分别插入objects中的元素
4.2NSMutableArray删除元素
- (void)removeLastObject
删除最后一个元素
- (void)removeAllObjects
删除所有的元素
-(void)removeObjectAtIndex:(NSUInteger)index
删除index位置的元素
-(void)removeObjectsAtIndexes:(NSIndexSet *)indexes
删除indexes位置的所有元素
- (void)removeObject:(id)anObject
删除特定的元素
- (void)removeObject:(id)anObjectinRange:(NSRange)range
在range范围内查找特定的元素进行删除
-(void)removeObjectsInArray:(NSArray *)otherArray
删除同时存在于otherArray和当前集合中的所有元素
-(void)removeObjectsInRange:(NSRange)range
删除range范围内的所有元素
4.3NSMutableArray替换元素
-(void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject
用anObject替换index位置对应的元素
-(void)replaceObjectsAtIndexes:(NSIndexSet *)indexes withObjects:(NSArray *)objects
用objects中的元素分别替换indexes对应位置的元素
-(void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray*)otherArray range:(NSRange)otherRange
用otherArray中otherRange范围内的元素替换当前集合range范围内的元素
-(void)replaceObjectsInRange:(NSRange)range withObjectsFromArray:(NSArray *)otherArray
用otherArray中的元素替换当前集合range范围内的元素
-(void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2
交换idx1和idx2位置的元素
NSSet常用方法
//返回元素个数
-(NSUInteger)count;
//object是否在集合中,若在的话就返回,否则返回nil
-(id)member:(id)object;
//返回集合的美剧
-(NSEnumerator *)objectEnumerator;
//返回一个数组
-(NSArray *)allObjects;
//返回任意的一个对象
-(id)anyObject;
//是否包含对象
-(BOOL)containsObject:(id)anObject;
//是否有交集
-(BOOL)intersectsSet:(NSSet *)otherSet;
//是否是子集
-(BOOL)isSubsetOfSet:(NSSet *)otherSet;
//所有集合中对象都执行aSelector方法
-(void)makeObjectsPerformSelector:(SEL)aSelector;
-(void)makeObjectsPerformSelector:(SEL)aSelector
withObject:(id)argument;
//所有集合中对象执行block
-(void)enumerateObjectsUsingBlock:(void (^)(id obj, BOOL *stop))block;
NSMutableSet常用方法
// 添加数组中对象到集合中
- (void)addObjectsFromArray:(NSArray *)array;
// 移除不存在otherSet中的对象
- (void)intersectSet:(NSSet *)otherSet;
// 移除otherSet中得对象
- (void)minusSet:(NSSet *)otherSet;
// 移除所有对象
- (void)removeAllObjects;
// 合并otherSet中得对象
- (void)unionSet:(NSSet *)otherSet;
NSDate的静态初始化
+ (id)date
返回当前时间
+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs
返回以当前时间为基准,然后过了secs秒的时间
+ (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs
返回以2001/01/01 GMT为基准,然后过了secs秒的时间
+ (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs
返回以1970/01/01 GMT为基准,然后过了secs秒的时间
+ (id)distantFuture
返回很多年以后的未来的某一天
+ (id)distantPast
返回很多年以前的某一天
NSDate的动态初始化
- (id)addTimeInterval:(NSTimeInterval)secs
返回以目前的实例中保存的时间为基准,然后过了secs秒的时间
- (id)init
初始化为当前时间。[NSDate date]
- (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs
初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间
- (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate
初始化为以refDate为基准,然后过了secs秒的时间
- (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs
初始化为以当前时间为基准,然后过了secs秒的时间
NSDate日期比较
- (BOOL)isEqualToDate:(NSDate *)otherDate
与otherDate比较,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate
与anotherDate比较,返回较早的那个日期
- (NSDate *)laterDate:(NSDate *)anotherDate
与anotherDate比较,返回较晚的那个日期
- (NSComparisonResult)compare:(NSDate *)other
该方法用于排序时调用:
. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending
NSDate取回时间间隔
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate
以refDate为基准时间,返回实例保存的时间与refDate的时间间隔
- (NSTimeInterval)timeIntervalSinceNow
以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔
- (NSTimeInterval)timeIntervalSince1970
以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔
- (NSTimeInterval)timeIntervalSinceReferenceDate
以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔
+ (NSTimeInterval)timeIntervalSinceReferenceDate
以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔
NSDate日期格式化
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
// 将NSString转换为NSDate
NSDate *date = [formatter dateFromString:@"2010-03-24 00:00:00"];
// 将NSDate转换为NSString
NSLog(@"%@", [formatter stringFromDate:date]);
反射
12.1Class的反射
通过类名的字符串形式实例化对象
Classclass = NSClassFromString(@"Student");
Student*stu = [[class alloc] init];
将类名变成字符串
Classclass = [Student class];
NSString*className = NSStringFromClass(class);
12.2SEL的反射
通过方法的字符串形式实例化方法
SELselector = NSSelectorFromString(@"setName:");
[stu performSelector:selectorwithObject:@"Mike"];
将方法变成字符串
NSStringFromSelector(@selector(setName:));