欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Object-C,数组NSArray

程序员文章站 2022-06-22 12:06:37
晚上回来,写了2个ios应用程序。 就是在界面中,展示标签。一种是手动构造界面,然后绑定事件。另外一种是,使用自带的界面作为容器,但是手动向里面放其它界面元素。   书中的观点是,使用图形...

晚上回来,写了2个ios应用程序。

就是在界面中,展示标签。一种是手动构造界面,然后绑定事件。另外一种是,使用自带的界面作为容器,但是手动向里面放其它界面元素。

 

书中的观点是,使用图形化界面,构造界面比较好。

 

然后,又写了个object-c数组的例子。

 

object-c相对简单一些,黑屏控制台输出,而ios可视化界面的程序,代码较多,也不好描述。

ios程序的“上下文环境”更复杂一些把,而object-c语言,和java就类似。

 

 

//
//  main.m
//  nsarraytest
//
//  created by fansunion on 15/12/1.
//  copyright (c) 2015年 demo. all rights reserved.
//

#import 

//演示不可变数组
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //不可变数组,用类方法构造数组
        nsarray* array =[nsarray arraywithobjects:@"a",@"b",@"c",nil];
        //访问元素有2种方式
        nslog(@"the first element is %@",array[0]);
        nslog(@"the second element is %@",[array objectatindex:1]);
        
        //不可变数组,在原来的基础上再增加一个元素d返回心的数组
        nsarray* newarray = [array arraybyaddingobject:@"d"];
        //使用for循环,打印新的数组
        for(int index=0;index

 

程序输出

 

2015-12-01 21:16:55.768 nsarraytest[5346:358824] the first element is a

2015-12-01 21:16:55.769 nsarraytest[5346:358824] the second element is b

2015-12-01 21:16:55.769 nsarraytest[5346:358824] the 0 element is a

2015-12-01 21:16:55.769 nsarraytest[5346:358824] the 1 element is b

2015-12-01 21:16:55.770 nsarraytest[5346:358824] the 2 element is c

2015-12-01 21:16:55.770 nsarraytest[5346:358824] the 3 element is d

2015-12-01 21:16:55.774 nsarraytest[5346:358824] the element is a

2015-12-01 21:16:55.774 nsarraytest[5346:358824] the element is b

2015-12-01 21:16:55.774 nsarraytest[5346:358824] the element is c

2015-12-01 21:16:55.774 nsarraytest[5346:358824] the element is d

program ended with exit code: 0

 

需要特别指出的是,nsarray是不可变的,就像java中的string对象。

nsmutablearray是可变数组。

 

这点和java中正好相反:java中的arraylist正好是可变的,如果想要不可变的,apache等第三方有实现。