- (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这一块 求指教!谢谢!
伊谢尔伦2017-04-24 09:13:31
It’s just an array setter/getter, but the syntax of OC is written like this. You should be able to see it clearly if written in other syntax.
This is equivalent to this writing method in C language
c
void setTire(Tire tire,int index){ tires[index]=tire; } Tire setTire(int index){ return tires[index]; }
or JAVA
java
public void setTire(Tire tire,int index){ tires[index]=tire; } public Tire tireAtIndex(int index){ return tires[index]; }
apple new language swift
swift
func setTire(tire:Tire,index:Int){ tires[index]=tire; } func tireAtIndex(index:Int)->Tire{ return tires[index]; }
JS with Arrow Function
javascript
var setTire=(tire,index)=>tires[index]=tire; var tireAtIndex=(index)=>tires[index];
迷茫2017-04-24 09:13:31
I’m also confused as to why someone writes code like this. And it’s a tutorial. Isn’t this misleading to newbies?
It’s not a Setter Getter method, it’s just two ordinary methods. The function of the method is similar to setting the object of a certain item in the array, and getting the object of a certain item
迷茫2017-04-24 09:13:31
Now you no longer need to define instance variables and write access methods, just use properties directly!
The tutorials you are watching are probably from 3 years ago.
For tutorials on properties, you can visit our blog about the introduction of properties