是否可以使用点语法调用方法? [英] Is it OK to use dot syntax to call methods?

查看:131
本文介绍了是否可以使用点语法调用方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何开发使用目标C,我读了这本书,从一个类访问一个ivar使用点语法(obj.var),你必须实现这些vars使用@properties,但我尝试使用这种访​​问,而不为这些vars定义@properties,它似乎工作正常。

I'm trying to learn how to develop using objective C and I read on this book that to access an ivar from a class using dot syntax (obj.var) you must implement these vars using @properties, however I've tried using this kind of access without defining @properties for these vars and it seemed to work normally.

这种访问如何工作?

例如:

ComplexNumber *c1 = [[ComplexNumber alloc]init];
c1.realPart = 3;
c1.imaginaryPart = 2;

ComplexNumber的方法:

ComplexNumber's methods:

- (double)modulus;
-(void)setRadius:(double)aRadius phase:(double)aPhase;
-(void)print;
-(double)realPart;
-(double)imaginaryPart;
-(void)setRealPart:(double)value;
-(void)setImaginaryPart:(double)value;


推荐答案

没有它不是一个好的做法,零参数方法使用点语法,但现在Xcode会警告你这样做。这是对苹果的编码指南。

No its not a good practice, you technically can access zero argument methods using dot syntax but now Xcode will warn you about doing this. This is against Apple's coding guidelines.

括号语法应该用于调用方法。

Bracket syntax should be used for calling methods.

这篇关于是否可以使用点语法调用方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆