Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

php - Objective-C Default Argument Value

Hey there, quick question here. I'm sure there's a simple answer.

Coming from PHP, I'm used to declaring a function with a default argument value like this:

function myFunction ($array, $sort = FALSE)  {

}

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?

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.

Thanks for your help guys :D

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...