swift中函数返回int型数组,在OC中调用函数为何不能用Int型数值接收返回值中的数组元素

iOS 码拜 9年前 (2015-05-11) 1216次浏览 0个评论

swift中编写函数

func returnTotalRouteX()->[Int]{
        println("call returnTotalRouteX")
        var routeX = [Int]()
        for item in totalRoute{
            routeX.append(nodeData[item]!.xPosition)
        }
        println(routeX)
        return routeX
    }

在OC中调用函数

    GetRoute *h = [[GetRoute alloc] init];
    
    if (h.returnTotalRouteX.count == 0) {
        NSLog(@"it is empty");
    }
    else{
        NSLog(@"%@",h.returnTotalRouteX);
        NSLog(@"%@",h.returnTotalRouteX[0]);
        NSLog(@"it is not empty");
    }

NSLog(@”%@”,h.returnTotalRouteX[0]);这行只有用%@才能正确输出,若是用%d则输出不正确。我想要获取该int型的数组元素,该如何做呢?

60分
应该是正常的。
Swift 是支持强类型的,在上面swift 中的方法返回的是一个Int类型的数组。而在oc中,数组是使用NSArray 来存储的。
在oc中通过下标来访问一个NSArray数组中的元素返回的是id类型,所以要使用%@来输出元素的值,而不是%d
如果你非要使用%d的话,就需要做一个类型转换了

id val = h.returnTotalRouteX[0];
int intVal = [val intValue];
NSLog(@"value is :%d",intVal);

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明swift中函数返回int型数组,在OC中调用函数为何不能用Int型数值接收返回值中的数组元素
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!