您的位置:首页 > 其它

C 语言和 OC 数组初始化的区别

2016-04-08 11:17 302 查看
 在项目中无意中看到一段代码引起了我的注意:

        UIBezierPath *tmpPath = [[UIBezierPath
alloc]
init];

        CLLocationCoordinate2D coordinates[[store.polygon_coordinates
count]];


        for(int i=0;i<[store.polygon_coordinates
count];i++) {

            coordinates[i].latitude = [[[store.polygon_coordinates
objectAtIndex:i]
objectAtIndex:0]
doubleValue];

            coordinates[i].longitude = [[[store.polygon_coordinates
objectAtIndex:i]
objectAtIndex:1]
doubleValue];

            if (i==0) {

                [tmpPath moveToPoint:[mapView
convertCoordinate:coordinates[i]
toPointToView:mapView]];

            } else {

                [tmpPath addLineToPoint:[mapView
convertCoordinate:coordinates[i]
toPointToView:mapView]];

            }

        }

        [tmpPath closePath];
看看我红色标出来的,一开始我还以为是二元数组,仔细一看发现里面只是一个调用的方法而已,所以最终会发现其实只是一个C 数组
在看下面直接把OC的经纬度值存进去,不由得暗自叹服。

我们OC可不能这么直接就能拿着用,我们必须,先初始化才能用

  NSMutableArray *a ;

        a[0] =
@1;

        NSLog(@"%@",a[0]);

假如你这么打印结果肯定是null

而C的初始化非常简单:

int a[10] ;

        a[0] =
1;

        

        NSLog(@"%i",a[0]);

就是第一句 就简简单单完成了对数组的初始化,换句话说,数组已经存在啦!

而OC中 NSMutableArray *a ; 这只是声明了一个数组。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: