概念区别

1:NSInteger 与 NSUInteger 和 int与 NSInteger 区别
先说说NSInteger 与 NSUInteger,在看书上代码是遇见NSInteger和NSUInteger,不知道什么时候用NSInteger,什么时候用 NSUInteger,在网上搜了一下,NSUInteger是无符号的整型, NSInteger是有符号的整型,在表视图应用中常见 NSUInteger row= [indexPath row];因为这是显示tableViewCell有多少个,只能是非零整数的,也就是说,NSUInteger常用于索引值;
 int与 NSInteger没有本质区别,只是一个定义问题,请看关于它们的定义:
#if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64
typedef long NSInteger;
typedef unsigned long NSUInteger;
#else
typedef int NSInteger;
typedef unsigned int NSUInteger;
#endif
2:@synthesize window=_window; 的理解
@synthesize window=_window; 意思是说,window 属性为 _window 实例变量合成访问器方法。
也就是说,window属性生成存取方法是setWindow,这个setWindow方法就是_window变量的存取方法,它操作的就是_window这个变量。
下面是一个常见的例子
@interface MyClass:NSObject{
      MyObjecct *_myObject;
}
@property(nonamtic, retain) MyObjecct *myObject;
@end
 
@implementatin MyClass
@synthesize myObject=_myObject;
这个类中声明了一个变量_myObject,又声明了一个属性叫myObject,然后用@synthesize生成了属性myObject的存取方法,这个存取方法的名字应该是:setmyObject和getmyObject。@synthesize myObject=_myObject的含义就是属性myObject的存取方法是做用于_myObject这个变量的。
这种用法在Apple的Sample Code中很常见,
 
来自:http://www.cocoachina.com/bbs/read.php?tid=66688&page=2
 
3:NSSet类型 以及与NSArray区别
NSSet到底什么类型,其实它和NSArray功能性质一样,用于存储对象,属于集合; NSSet  , NSMutableSet类声明编程接口对象,无序的集合,在内存中存储方式是不连续的,不像NSArray,NSDictionary(都是有序的集合)类声明编程接口对象是有序集合,在内存中存储位置是连续的;
 
        NSSet和我们常用NSArry区别是:在搜索一个一个元素时NSSet比NSArray效率高,主要是它用到了一个算法hash(散列,也可直译为哈希);开发文档中这样解释:You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.
比如你要存储元素A,一个hash算法直接就能直接找到A应该存储的位置;同样,当你要访问A时,一个hash过程就能找到A存储的位置。而对于NSArray,若想知道A到底在不在数组中,则需要便利整个数组,显然效率较低了;
 
       NSSet,NSArray都是类,只能添加cocoa对象,如果需要加入基本数据类型(int,float,BOOL,double等),需要将数据封装成NSNumber类型。
 
NSSet 常用方法总结
+(id)setWithObjects:obj1,obj2,...nil
使用一组对象创建新的集合
-(id)initWithObjects:obj1,obj2,....nil
使用一组对象初始化新分配的集合
-(NSUInteger)count
返回集合成员个数
-(BOOL)containsObject:obj
确定集合是否包含对象 obj
-(BOOL)member:obj
确定集合是否包含对象 obj
-(NSEnumerator*)objectEnumerator
返回集合中所有对象到一个 NSEnumerator 类型的对象
-(BOOL)isSubsetOfSet:nsset
判断集合是否是NSSet的子集
-(BOOL)intersectsSet:nsset
判断两个集合的交集是否至少存在一个元素
-(BOOL)isEqualToSet:nsset
判断两个集合是否相等
 
 
NSMutableSet 常用方法总结
-(id)setWithCapcity:size
创建一个有size大小的新集合
-(id)initWithCapcity:size
初始化一个新分配的集合,大小为size
-(void)addObject:obj
添加对象 obj 到集合中
-(void)removeobject:obj
从集合中删除对象 obj
-(void)removeAllObjects
删除集合中所有对象
-(void)unionSet:nsset
将nsset的所有元素添加到集合
-(void)minusSet:nsset
从集合中去掉所有的NSSet 的元素
-(void)interectSet:nsset
集合和NSSet 做交集运算
 
initWithFrame 和 initWithCoder
initWithFrame  和   initWithCoder
 
当我们所写的程序里没用用Nib文件(XIB)时,用代码控制视图内容,需要调用initWithFrame去初始化
 
- (id)initWithFrame:(CGRect)frame
{
    if (self =[superinitWithFrame:frame]) {
        // 初始化代码
        }
    returnself;
}
 
用于视图加载nib文件,从nib中加载对象实例时,使用 initWithCoder初始化这些实例对象
- (id)initWithCoder:(NSCoder*)coder
{
    if (self =[superinitWithcoder:coder]) {
        // 初始化代码
        }
    returnself;
}
 
这些区别需要不断更新

posted @ 2013-08-09 18:08  0xdbe  阅读(227)  评论(0编辑  收藏  举报