objective-c开发问题~新手

浏览:24日期:2023-12-16

问题描述

- (void) setTire: (Tire *) tire atIndex: (int) index{ [tires replaceObjectAtIndex: index withObject: tire];} // setTire:atIndex:- (Tire *) tireAtIndex: (int) index{ Tire *tire; tire = [tires objectAtIndex: index]; return (tire);} // tireAtIndex:

这是objective-c基础教程里面的代码我就是不理解这段什么意思 尤其是(Tire *) tireAtIndex: (int) index这一块 求指教!谢谢!

问题解答

回答1:

就是个数组的setter/getter,只不过OC的语法就这样写,用其他语法写出来你应该能看清楚了。相当于C语言的这种写法

cvoid setTire(Tire tire,int index){ tires[index]=tire;}Tire setTire(int index){ return tires[index];}

或者JAVA

javapublic void setTire(Tire tire,int index){ tires[index]=tire;}public Tire tireAtIndex(int index){ return tires[index];}

apple新语言swift

swiftfunc setTire(tire:Tire,index:Int){ tires[index]=tire;}func tireAtIndex(index:Int)->Tire{ return tires[index];}

JS with Arrow Function

javascriptvar setTire=(tire,index)=>tires[index]=tire;var tireAtIndex=(index)=>tires[index];回答2:

我也很困惑为啥有人这样写代码。而且还是教程,这不是误导新手吗不是啥Setter Getter方法,就是普通的两个方法,方法的功能就是类似设置数组的某项的object,和获取某项的object

回答3:

现在已经不在需要定义实例变量以及编写存取方法了,直接使用属性property搞定!你看的这些教程估计是3年以前的教程了。

有关属性的教程,可以访问下我们的博客上关于属性propery介绍

相关文章: