嘿,这里有一个简短的问题.我相信有一个简单的答案.
Hey there, quick question here. I'm sure there's a simple answer.
来自 PHP,我习惯于用这样的默认参数值声明一个函数:
Coming from PHP, I'm used to declaring a function with a default argument value like this:
function myFunction ($array, $sort = FALSE) {
}
我的 sort 参数没有被填充,函数将继续使用默认值 false.在Obj-C中,有没有类似的东西?
I the sort parameter wasn't filled, the function would continue with the default value of false. In Obj-C, is there a similar thing?
我正在完成Objective-C 2.0 中的编程"一书中的练习,它希望我重新编写分数类打印函数以默认不减少分数,但如果值为 TRUE给出减少,继续减少分数,然后打印.本章(本书中也没有)提供了有关此的任何信息.
I'm working through the exercises in my "Programming In Objective-C 2.0" book, and it wants me to re-write a fraction class print function to default-ly not reduce the fraction, but if the value TRUE for reduce is given, go ahead and reduce the fraction, then print. The chapter (Nor nowhere in the book) gives any information on this.
感谢您的帮助:D
Objective-C 本身不存在默认参数.他们真的不能,因为参数数量与方法名称密不可分——每个冒号对应一个参数.
Default arguments don't exist in Objective-C, per se. They can't really, because the argument count is inextricably tied to the method name — each colon corresponds to one argument.
Objective-C 程序员通过创建方便"的方法来实现类似的目标,这些方法只调用更原始"的方法,其中一些参数填充有默认值.例如,-[NSArray indexOfObject:]
可以实现为 -[NSArray indexOfObject:inRange:]
的版本,参数为 NSMakeRange(0, [selfcount])
用于 inRange:
部分.
Objective-C programmers accomplish a similar goal, though, by creating "convenience" methods that just call to a more "primitive" method with some of the arguments filled in with default values. For example, -[NSArray indexOfObject:]
could be implemented as version of -[NSArray indexOfObject:inRange:]
with an argument of NSMakeRange(0, [self count])
for the inRange:
part.
不过,在这种情况下,我不认为你的书在谈论这个.我认为这只是意味着如果为 reduce:
参数给出 YES 则减少分数,如果给出 NO 则不减少它.
In this case, though, I don't think your book is talking about that. I think it simply means to reduce the fraction if YES is given for the reduce:
argument and not reduce it if NO is given.
这篇关于Objective-C 默认参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!