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

6 篇文章 0 订阅

 在项目中无意中看到一段代码引起了我的注意:

        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 ; 这只是声明了一个数组。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值