实例方法和语法
我只是想知道这两种类型的语法是否有区别
假设我有类似的东西..
NSString *Jam = [[NSString alloc]init];
或者
NSString *Jam;
哪个并不重要..
执行以下两行代码有什么区别..
Jam = [Jam substringToIndex:1];
以及
[Jam substringToIndex:1];
为什么会这样我发现只有 NSString 能够提取这样的东西..
如果我使用到目前为止使用过的任何其他类(我没有使用过那么多),这种类型的语法不起作用。
而对于 NSString 类,我可以这样做
NSString *object = [object stringByAppendingFormat:@"%@"];
,或者
NSString *object;
object = [object stringByAppendingFormat:@"%@"];
但是对于任何其他类.. 比如说 NSInteger,如果我尝试相同的语法..
NSInteger *number = [number setIntValue:2];
我会得到一个错误,告诉我 void 值不会被忽略,因为它应该是。
谢谢一大堆。
I'm just wondering if there is a difference in these two types of syntax
Say I have something like this ..
NSString *Jam = [[NSString alloc]init];
or
NSString *Jam;
doesn't matter which..
what's the difference between doing the following two lines of code..
Jam = [Jam substringToIndex:1];
and
[Jam substringToIndex:1];
and why is it that I find only NSString to be able to pull of something like this..
if I used any other class that I've used so far (I haven't worked with that many), this type of syntax does not work.
Whereas with NSString class I can do
NSString *object = [object stringByAppendingFormat:@"%@"];
or
NSString *object;
object = [object stringByAppendingFormat:@"%@"];
but with any other class.. say , NSInteger, if I try the same syntax..
NSInteger *number = [number setIntValue:2];
I'd get an error telling me void value is not ignored as it ought to be.
Thank's a bunch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它适用于 NSString ,因为 stringByAppendingFormat 返回一个新的 NSString ,它是通过将新字符串附加到原始字符串而创建的。
setIntValue
不会返回类的新副本,这就是您收到错误的原因。It works for
NSString
becausestringByAppendingFormat
returns a newNSString
which is created by appending new string to the original one.setIntValue
does not return new copy of a class that's why you are getting the error.