Objective-C Default Argument Value

前端 未结 6 1741
广开言路
广开言路 2020-12-10 03:58

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:

6条回答
  •  执笔经年
    2020-12-10 04:31

    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.

提交回复
热议问题