1

I am creating an application that needs to find out if the current date is in one of several date ranges, and depending on which one it is in, it needs to set an enum to a value. I have the following code, but I don't know how to call the function with multiple arguments. Also, what is the difference between starting a function with a + or - sign?

+ (BOOL)rangeFinder:(NSDate*)date isBetweenDate:(NSDate*)beginDate andDate:        (NSDate*)endDate
{
    if ([date compare:beginDate] == NSOrderedAscending)
         return NO;

    if ([date compare:endDate] == NSOrderedDescending) 
        return NO;

    return YES;
}

Thanks,

Hersh

Hersh Bhargava
  • 615
  • 4
  • 19
  • 1
    http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html#//apple_ref/doc/uid/TP30001163-CH11-SW1 – Vin Nov 04 '11 at 11:04

4 Answers4

1

A method starting with '+' denotes the method is a class method so you don't need an instance to call it... i.e.

[MyClass someMethod];

As opposed to method starting with '-' which denotes an instance method ....

MyClass *object = [[MyClass alloc] init];
[object someMethod];

As for calling methods with multiple params....

BOOL result = [MyClass rangeFinder:myDate isBetweenDate:firstDate andDate:secondDate];
Simon Lee
  • 22,304
  • 4
  • 41
  • 45
1

refer this links Objective-C: Class vs Instance Methods?
Variable arguments in Objective-C methods

Community
  • 1
  • 1
Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
0
[YourClass rangeFinder:aDate isBetweenDate:anotherDate andDate:yetAnotherDate];

A method that starts with a + is class method. That is, it will be called without an instance of the object (self will point to the equivalent of [YourClass class]):

[YourClass myPlusMethod]

A method that starts with a - is an instance method. It gets called on an instance (object) of your class:

[yourInstance myMinusMethod]

Take the typical initialization:

yourInstance = [[YourClass alloc] init];

The alloc is a class method and returns an instance. On that instance, you're calling init.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
0

When a method begins with a +, it signifies that the method is a class method. This means that instead of calling it like:

// Calling an instance method.
[self doSomething];

you will call it like:

// Calling a class method.
[ClassName doSomething];

You don't need an instance of an object to call a class method. You might think this means that the code will run faster because you don't need to hold on to an object, but as far as I know, the runtime will actually create an object on the fly to execute your method, resulting in dramatic unresponsiveness if you frequently (thousands of times) call a class method. On the other hand, methods that start with a - are instance methods. In order to call them, you first need to create an instance of the object. For example:

// Create the object.
SomeObject *tSomeObject = [[SomeObject alloc] init];

// Calling the method.
[tSomeObject doSomething];

Calling a method with multiple arguments works the same way as calling a single argument method. Here's how it works:

[ClassName rangeFinder:date1 isBetweenDate:date2 andDate:date3];

I would consider changing the method signature from rangeFinder:isBetweenDate:andDate: to something more like date:isBetweenDate:andDate:. You may also want to consider the option of date3 being less than date2. Currently your implementation would return NO but it seems like you would want it to return YES.

FreeAsInBeer
  • 12,937
  • 5
  • 50
  • 82