NSArray 函数整理

2024-04-21 12:58
文章标签 函数 整理 nsarray

本文主要是介绍NSArray 函数整理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

    /*******************************************************************************************NSArray*******************************************************************************************//*---------------------------创建数组------------------------------*///NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];self.dataArray = array;[array release];//- (unsigned) Count;数组所包含对象个数;NSLog(@"self.dataArray cound:%d",[self.dataArray count]);//- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);/*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/    //arrayWithArray://NSArray *array1 = [NSArray alloc] init];NSMutableArray *MutableArray = [NSMutableArray alloc] init];NSArray *array = [NSArray arrayWithObjects:@"a",@"b",@"c",nil];NSLog(@"array:%@",array);MutableArray = [NSMutableArray arrayWithArray:array];NSLog(@"MutableArray:%@",MutableArray);array1 = [NSArray arrayWithArray:array];NSLog(@"array1:%@",array1);//Copy//id obj;NSMutableArray *newArray = [NSMutableArray alloc] init];NSArray *oldArray = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];NSLog(@"oldArray:%@",oldArray);for(int i = 0; i < [oldArray count]; i++){        obj = [oldArray objectAtIndex:i] copy];[newArray addObject: obj];}//     NSLog(@"newArray:%@", newArray);[newArray release];//快速枚举//NSMutableArray *newArray = [NSMutableArray alloc] init];NSArray *oldArray = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];    NSLog(@"oldArray:%@",oldArray);for(id obj in oldArray){[newArray addObject: obj];}//     NSLog(@"newArray:%@", newArray);[newArray release];    //Deep copy//NSMutableArray *newArray = [NSMutableArray alloc] init];NSArray *oldArray = [NSArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];    NSLog(@"oldArray:%@",oldArray);    newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);NSLog(@"newArray:%@", newArray);[newArray release];    //Copy and sort//NSMutableArray *newArray = [NSMutableArray alloc] init];NSArray *oldArray = [NSArray arrayWithObjects:@"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];    NSLog(@"oldArray:%@",oldArray);NSEnumerator *enumerator;enumerator = [oldArray objectEnumerator];id obj;while(obj = [enumerator nextObject]){[newArray addObject: obj];}[newArray sortUsingSelector:@selector(compare:)];NSLog(@"newArray:%@", newArray);[newArray release];/*---------------------------切分数组------------------------------*///从字符串分割到数组- componentsSeparatedByString:NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];NSLog(@"string:%@",string);    NSArray *array = [string componentsSeparatedByString:@","];NSLog(@"array:%@",array);[string release];//从数组合并元素到字符串- componentsJoinedByString:NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];NSString *string = [array componentsJoinedByString:@","];NSLog(@"string:%@",string);/*******************************************************************************************NSMutableArray*******************************************************************************************//*---------------给数组分配容量----------------*///NSArray *array;array = [NSMutableArray arrayWithCapacity:20];/*--------------在数组末尾添加对象----------------*///- (void) addObject: (id) anObject;//NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];[array addObject:@"Four"];NSLog(@"array:%@",array);/*--------------删除数组中指定索引处对象----------------*/    //-(void) removeObjectAtIndex: (unsigned) index;    //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];[array removeObjectAtIndex:1];NSLog(@"array:%@",array);/*-------------数组枚举---------------*/    //- (NSEnumerator *)objectEnumerator;从前向后//NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];NSEnumerator *enumerator;enumerator = [array objectEnumerator];id thingie;while (thingie = [enumerator nextObject]) {NSLog(@"thingie:%@",thingie);}//- (NSEnumerator *)reverseObjectEnumerator;从后向前//NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];NSEnumerator *enumerator;enumerator = [array reverseObjectEnumerator];id object;while (object = [enumerator nextObject]) {NSLog(@"object:%@",object);}//快速枚举//NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];for(NSString *string in array){NSLog(@"string:%@",string);}/*******************************************************************************************NSDictionary*******************************************************************************************//*------------------------------------创建字典------------------------------------*///- (id) initWithObjectsAndKeys;//NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];NSString *string = [dictionary objectForKey:@"One"];NSLog(@"string:%@",string);NSLog(@"dictionary:%@",dictionary);[dictionary release];/*******************************************************************************************NSMutableDictionary*******************************************************************************************//*------------------------------------创建可变字典------------------------------------*/    //创建NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];//添加字典[dictionary setObject:@"One" forKey:@"1"];[dictionary setObject:@"Two" forKey:@"2"];[dictionary setObject:@"Three" forKey:@"3"];[dictionary setObject:@"Four" forKey:@"4"];NSLog(@"dictionary:%@",dictionary);//删除指定的字典[dictionary removeObjectForKey:@"3"];NSLog(@"dictionary:%@",dictionary);/*******************************************************************************************NSValue(对任何对象进行包装)*******************************************************************************************//*--------------------------------将NSRect放入NSArray中------------------------------------*/    //将NSRect放入NSArray中NSMutableArray *array = [NSMutableArray alloc] init];NSValue *value;CGRect rect = CGRectMake(0, 0, 320, 480);    value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];[array addObject:value];NSLog(@"array:%@",array);//从Array中提取value = [array objectAtIndex:0];[value getValue:&rect];NSLog(@"value:%@",value);/*******************************************************************************************从目录搜索扩展名为jpg的文件*******************************************************************************************///NSFileManager *fileManager = [NSFileManager defaultManager];NSString *home;home = @"../Users/";NSDirectoryEnumerator *direnum;direnum = [fileManager enumeratorAtPath: home];NSMutableArray *files = [NSMutableArray alloc] init];//枚举NSString *filename;while (filename = [direnum nextObject]) {if([filename pathExtension] hasSuffix:@"jpg"]){[files addObject:filename];}}//快速枚举//for(NSString *filename in direnum)//{//    if([filename pathExtension] isEqualToString:@"jpg"]){//        [files addObject:filename];//    }//}NSLog(@"files:%@",files);//枚举NSEnumerator *filenum;filenum = [files objectEnumerator];while (filename = [filenum nextObject]) {NSLog(@"filename:%@",filename);}//快速枚举//for(id object in files)//{//    NSLog(@"object:%@",object);//} 

这篇关于NSArray 函数整理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/923139

相关文章

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

数论入门整理(updating)

一、gcd lcm 基础中的基础,一般用来处理计算第一步什么的,分数化简之类。 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } <pre name="code" class="cpp">LL lcm(LL a, LL b){LL c = gcd(a, b);return a / c * b;} 例题:

C++操作符重载实例(独立函数)

C++操作符重载实例,我们把坐标值CVector的加法进行重载,计算c3=c1+c2时,也就是计算x3=x1+x2,y3=y1+y2,今天我们以独立函数的方式重载操作符+(加号),以下是C++代码: c1802.cpp源代码: D:\YcjWork\CppTour>vim c1802.cpp #include <iostream>using namespace std;/*** 以独立函数

函数式编程思想

我们经常会用到各种各样的编程思想,例如面向过程、面向对象。不过笔者在该博客简单介绍一下函数式编程思想. 如果对函数式编程思想进行概括,就是f(x) = na(x) , y=uf(x)…至于其他的编程思想,可能是y=a(x)+b(x)+c(x)…,也有可能是y=f(x)=f(x)/a + f(x)/b+f(x)/c… 面向过程的指令式编程 面向过程,简单理解就是y=a(x)+b(x)+c(x)

利用matlab bar函数绘制较为复杂的柱状图,并在图中进行适当标注

示例代码和结果如下:小疑问:如何自动选择合适的坐标位置对柱状图的数值大小进行标注?😂 clear; close all;x = 1:3;aa=[28.6321521955954 26.2453660695847 21.69102348512086.93747104431360 6.25442246899816 3.342835958564245.51365061796319 4.87

OpenCV结构分析与形状描述符(11)椭圆拟合函数fitEllipse()的使用

操作系统:ubuntu22.04 OpenCV版本:OpenCV4.9 IDE:Visual Studio Code 编程语言:C++11 算法描述 围绕一组2D点拟合一个椭圆。 该函数计算出一个椭圆,该椭圆在最小二乘意义上最好地拟合一组2D点。它返回一个内切椭圆的旋转矩形。使用了由[90]描述的第一个算法。开发者应该注意,由于数据点靠近包含的 Mat 元素的边界,返回的椭圆/旋转矩形数据

Unity3D 运动之Move函数和translate

CharacterController.Move 移动 function Move (motion : Vector3) : CollisionFlags Description描述 A more complex move function taking absolute movement deltas. 一个更加复杂的运动函数,每次都绝对运动。 Attempts to

rtmp流媒体编程相关整理2013(crtmpserver,rtmpdump,x264,faac)

转自:http://blog.163.com/zhujiatc@126/blog/static/1834638201392335213119/ 相关资料在线版(不定时更新,其实也不会很多,也许一两个月也不会改) http://www.zhujiatc.esy.es/crtmpserver/index.htm 去年在这进行rtmp相关整理,其实内容早有了,只是整理一下看着方

笔记整理—内核!启动!—kernel部分(2)从汇编阶段到start_kernel

kernel起始与ENTRY(stext),和uboot一样,都是从汇编阶段开始的,因为对于kernel而言,还没进行栈的维护,所以无法使用c语言。_HEAD定义了后面代码属于段名为.head .text的段。         内核起始部分代码被解压代码调用,前面关于uboot的文章中有提到过(eg:zImage)。uboot启动是无条件的,只要代码的位置对,上电就工作,kern

JavaScript整理笔记

JavaScript笔记 JavaScriptJavaScript简介快速入门JavaScript用法基础语法注释关键字显示数据输出innerHTML innerText属性返回值的区别调试 数据类型和变量数据类型数字(Number)字符串(String)布尔值(Boolean)null(空值)和undefined(未定义)数组(Array)对象(Object)函数(Function) 变量