IOS学习之NSArray和NSMutableArray详解

来源:互联网 发布:用友软件使用说明 编辑:程序博客网 时间:2024/06/05 15:42

关于IOS的开发过程中对于数据的处理主要涉及到字典(dictionary),数组(array),字符串(string)的操作是最多的,所以很有必要对这三个家伙搞熟悉来!

今天先给大家带来数组的使用

个人认为关于数据的操作无非就是查 插 该 删的操作,所以掌握好中心就可以更快的熟悉啦!

首先针对NSArray的操作

首先是初始化操作

NSArray *array = [[NSArray alloc] array]; // creats and returns an empty array

array = [NSArray arrayWithObjects:@"ceshi",@"test",nil]; //ending with nil


array = [NSArray arrayWithArray:array1];

//creats and ruturns an array containing the objects in another given array


+(NSArray *)arrayWithContentsOfFile:(NSString *)path

//creats and returns an array containing the contents of file specified by a given path

//通过给的路径将该文件的内容创建并返回一个数组

//其中的path是由通过writeToFile:atomically:方法产生的路径值


+(NSArray *)arrayWithContentsOfURL:(NSURL *)url

//creates and returns an array containing the contents specified by a given URL

//创建并返回由给定的URL指定的内容数组


+(instanceType)arrayWithObject:(id)anObject

//creates and returns an array containing a given object

//创建并返回由给定对象所组成的数组


数组查询操作(Querying an Array)


-(BOOL)containsObject:(id)object

//Returns a Boolean value that indicates whether a given object is present in the array

//判断所给定的对象是否存在数组中


@property(readonly) NSUInteger count

//the number of objects in the array


@property(nonatomic,readonly) id firstobject

//the first object in the array(read-only)


@property(nonatomic,readonly) id lastObject

//the last object in the array


-(id)objectAtIndex:(NSUInteger)index

//returns the object located at the specified index


-(NSEnumerator *)objectEnumerator

//an enumerator object that lets you access each object in the array

//返回数组对象的枚举


-(NSUInteger)indexOfObject:(id)anObject

//returns the lowest index whose corresponding array value is equal to a given object

//返回最先出现给定对象所对应的索引值



比较

-(BOOL)isEqualToArray:(NSArray *)otherArray //判断两个数组是否相等


-(BOOL)writeToFile:(NSString *)path automically:(BOOL)flag //将数组的内容写进指定的路径中的文件


-(BOOL)writeToURL:(NSURL*)url atomically:(BOOL)flag //将数组的内容写入指定的特殊的URL


下面来介绍下NSMutableArray

首先也是初始化的操作


+(instanceType)arrayWithCapacity:(NSUinteger)numItems //初始化可变数组的容量(分配内存)

+(NSMutableArray*)arrayWithContentOfFile:(NSString *)path 

+(NSMutableArray *)arrayWithContentsOfURL:(NSURL *)url


添加操作

-(void)addObject:(id)object  //inserts a given object at the end of the array


-(void)addObjectsFromArray:(NSArray *)otherArray

 //Adds the objects contained in another given array to the end of the receving array's content


-(void)insertObject:(id)anObject atIndex:(NSUInteger)index

//inserts a given object into the array's contents at a given index


移除对象

-(void)removeAllObjects //Empties the array of all its elements


-(void)removeLastObject  //Removes the object with the highest-vlaues index in the array

-(void)removeObject:(id)anObject //Removes all occurrences in the array of a given object

-(void)removeObjectAtIndex:(NSUinteger)index //Removes the object at index


替换对象

-(void)replaceObjectAtIndex:(NSUinteger)index withObject:(id)anobject

//Replaces the object at index with anObject


-(void)setArray:(NSArray*)otherArray

//sets the receiving array's elements to those in another given array


暂时先总结到这来,希望自己可以好好加油,将IOS搞定


0 0
原创粉丝点击