Foundation Kit常用类介绍

Foundation Kit是OS X类库和IOS类库共享的基础类库,里面提供了很多封装类,具体可以见https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/ObjC_classic/,下面介绍一些常用类。

1.字符串类:NSString和NSMutableString。

NSMutableString继承于NSString,两者的区别是:NSMutableString是可变的,而NSString是不可变的。也就是说当字符串已经确定下来,NSString不能对这个字符串进行删除和添加操作,而NSMutableString就是解决这个问题的。

如下代码:

  NSString* pStr1 = @"hello";

  [pStr1 stringByAppendingString:@" world"]; //pStr1还是hello

  NSMutableString* pMTStr = [NSMutableString stringWithFormat: @"hello"];

  [pMTStr appendString: @" world"]; //pMTStr为hello world

pStr1调用方法stringByAppendingString后pStr1还是hello,pMTStr调用方法 appendString后pMTStr直接变为hello world。那么我们如何让pStr1变为hello world呢?很简单:pStr1 = [pStr1 stringByAppendingString:@" world"],这样就达到了pMTStr appendString相同的效果。

2.动态数组:NSArray和NSMutableArray,两者区别类似NSString和NSMutableString。动态数组类似于stl里面的vector。

3.链表:我没有找到类似于C++ STL里面的list实现,有知道的同学请告诉下我。

4.set集合: NSSet, NSMutableSet, and NSCountedSet。

一开始我还以为NSSet跟C++的set差不多,看文档之后才发现,NSSet不会对对象进行排序,但是NSSet会保证对象的唯一性。

    
    NSString *pStr1 = @"c";
    NSString *pStr2 = @"b";
    NSString *pStr3 = @"a";
    NSString *pStr4 = @"c";
    NSSet* mySet = [NSSet setWithObjects:pStr1, pStr2, pStr3, pStr4, nil];

上面代码里面mySet里面的对象为 b,c,a。

5.map实现:NSDictionary和NSMutableDictionary,类似于C++里面的map。

6.疑问:OC里面有类似C++里面的multiset、multimap、list、stack、deque实现吗?目前我在文档里面没有查到类似类,有知道的朋友可以告诉下我,谢谢!

原文地址:https://www.cnblogs.com/52xpz/p/4248644.html