Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 440 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I pass multiple parameters in Objective-C?

#1
I have read several of the post about Objective-C method syntax but I guess I don't understand multiple names for a method.

I'm trying to create a method called `getBusStops` with `NSString` and `NSTimeInterval` parameters and a return type of `NSMutableArray`. This is how I have constructed the method but it obviously gets errors at runtime:

- (NSMutableArray *)getBusStops:(NSString *)busStop
(NSTimeInterval *)timeInterval;


I saw another example with a method:

-(NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component

I don't understand why this method has a method name for each parameter. Should I do the same with something like:

- (NSMutableArray *)getBusStops:(NSString *)busStop
forTime:(NSTimeInterval *)timeInterval

Reply

#2
Objective-C doesn't have named parameters, so everything on the left side of a colon is part of the method name. For example,

getBusStops: forTime:

is the name of the method. The name is broken up so it can be more descriptive. You could simply name your method

getBusStops: :

but that doesn't tell you much about the second parameter.
Reply

#3
Yes; the Objective-C method syntax is like this for a couple of reasons; one of these is so that it is clear what the parameters you are specifying are. For example, if you are adding an object to an `NSMutableArray` at a certain index, you would do it using the method:

- (void)insertObject:(id)anObject atIndex:(NSUInteger)index;

This method is called `insertObject:atIndex:` and it is clear that an object is being inserted at a specified index.

In practice, adding a string "Hello, World!" at index 5 of an `NSMutableArray` called `array` would be called as follows:

NSString *obj = @"Hello, World!";
int index = 5;

[array insertObject:obj atIndex:index];

This also reduces ambiguity between the order of the method parameters, ensuring that you pass the object parameter first, *then* the index parameter. This becomes more useful when using functions that take a large number of arguments, and reduces error in passing the arguments.

Furthermore, the method naming convention is such because Objective-C doesn't support overloading; however, if you want to write a method that does the same job, but takes different data-types, this can be accomplished; take, for instance, the `NSNumber` class; this has several object creation methods, including:

- `+ (id)numberWithBool:(BOOL)value;`
- `+ (id)numberWithFloat:(float)value;`
- `+ (id)numberWithDouble:(double)value;`

In a language such as C++, you would simply overload the number method to allow different data types to be passed as the argument; however, in Objective-C, this syntax allows several different variants of the same function to be implemented, by changing the name of the method for each variant of the function.




Reply

#4
The text before each parameter is **part** of the method name. From your example, the name of the method is actually

-getBusStops:forTime:

Each : represents an argument. In a method call, the method name is split at the :s and arguments appear after the :s. e.g.

[getBusStops: arg1 forTime: arg2]
Reply

#5
(int) add: (int) numberOne plus: (int) numberTwo ;
(returnType) functionPrimaryName : (returnTypeOfArgumentOne) argumentName functionSecondaryNa

me:

(returnTypeOfSecontArgument) secondArgumentName ;

as in other languages we use following syntax
void `add(int one, int second)`
but way of assigning arguments in `OBJ_c` is different as described above
Reply

#6
for create method:

-(void)mymethods:(NSString *)aCont withsecond:(NSString *)a-second {
//method definition...
}

for call the method:

[mymethods:self.contoCorrente withsecond:self.asecond];
Reply

#7
You need to delimit each parameter name with a ":" at the very least. Technically the name is optional, but it is recommended for readability. So you could write:

- (NSMutableArray*)getBusStops:(NSString*)busStop :(NSTimeInterval*)timeInterval;

or what you suggested:

- (NSMutableArray*)getBusStops:(NSString*)busStop forTime:(NSTimeInterval*)timeInterval;
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through