-2

I am following some examples from a text book regarding an objects but somehow along the way I completely missed some sort of syntax style or rule and I am not sure of what I'm looking at here.

These two methods are what I'm a bit stuck on. I'm not sure how I should be reading them in the literal sense.

Is this a string function that accepts a pointer to a string object we call lastTimeString

-(NSString *)lastTimeString;  

and

I'm not sure what the colon is for. Is this a method that accepts a pointer to a NSTimer object ?

   -(void)updateLastTime: (NSTimer *)t;

Please help. The formatting or style is just a little jarring to me at the moment.

#import <Foundation/Foundation.h>
@interface BNRLogger : NSObject

@property (nonatomic) NSDate *lastTime;
-(NSString *)lastTimeString;
-(void)updateLastTime: (NSTimer *)t;

@end
redchannel
  • 13
  • 2
  • 5

5 Answers5

1

the part that comes after either + or - in brackets is the function's return type. i.e.

- (NSString *)iReturnAString;
+ (int)andIReturnAnInt

the colon inidcates one input parameter. the whole function syntax looks like this

-/+(<return type>)functionNameStarts:(<parameter 1 type>)<parameter 1 name> functionNameContinues:(<parameter 2 type)<parameter 2 name> functionNameEnds:(<parameter 3 type>)<parameter 3 name>;

you can have as many input parameters as you want. so for a function

- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;

the name of the function is

scrollToRowAtIndexPath: atScrollPosition: animated:

and it takes 3 parameters, which are:

NSIndexPath* indexPath, UITableViewScrollPosition scrollPosition, BOOL animated

hope this helps

kap
  • 842
  • 4
  • 16
0
-(NSString *)lastTimeString;

This is a function declaration that will return you a NSString type variable.

Example: NSString *myString = [self lastTimeString];

-(void)updateLastTime: (NSTimer *)t;

This is also a function declaration that will allow you pass a NSTimer type variable into.

Example: [self updateLastTime:myTimerObj];

Here are the Basics:

Methods with no parameter

<method type> (<return type>) <method name>;

+ (void) doLogin;

- (void) doLogin;

Methods with a single parameter

<method type> (<return type>) <method name>: (<argument type>) <argument name>;

+(void) doLoginWithUserId: (NSString *) userId;

- (void) doLoginWithUserId: (NSString *) userId;

Methods with 2 parameters

<method type> (<return type>) <method name>: (<argument type>) <argument name> <argument 2 label>: (<argument 2 type>) <argument 2 name>;

+(void) doLoginWithUserId: (NSString *) userId andPassword : (NSString *) pwd;

- (void) doLoginWithUserId: (NSString *) userId andPassword : (NSString *) pwd;

Methods with a single NSString return value

<method type> (<return type>) <method name>;

+(NSString *) doLogin;

- (NSString *) doLogin;
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
0

-(NSString *)lastTimeString;
this is a function declaration whose return type is NSString. it will return a time string.

-(void)updateLastTime: (NSTimer *)t;

this is a function to update the time it will not return anything so its return type is void to call this function we have to pass timer as argument [self updateLastTime:timer]

Karan Dua
  • 2,401
  • 3
  • 15
  • 17
0

I will try to compare C-function and Objective-C function syntax, hope this will help you to understand.

Following accepts nothing and returns nothing:

void function(); //C
-(void) function; //Objective-C

Following returns nothing and accepts one integer value.

void function(int value); //C
-(void) function:(int)value; //Objective-C

Following returns nothing and accepts two arguments one integer value and one float value each. Notice in objective-c the method name gets changed. Infact for each extra parameter we add some meaningful word to the method name.

void function(int iValue, float fValue); //C
-(void) function:(int)iValue
     otherValue:(float)fValue; //Objective-C

If you wish you return any value, then simply use the return type value in place of void.

Also, in Objective-C we have two kinds of methods one starts with - known as instance method, other starts with + known as class method.

Instance method can be called only from an object of the class whereas class method is called without creating the instance of the class.

E.g :

Yourclass *object = ... ;
NSString *somevalue1 = [object method]; //this is an instance method since called by object
NSString *somevalue2 = [YourClass cMethod]; //cMethod called without instance, directly with className
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
0

Highlight:

There's mostly two files of a class, first header file (classname.h) and second implementation file (classname.m). Your code I'm taken in to consideration which are of header file. I'm going to explain you each line with reference links. Let me know if you'll still doubt in that.


#import <Foundation/Foundation.h> 

Import section, here you should import (written as #import) any frame work or classes to access their public methods, ivars, blocks or other properties here. In this example, we import Foundation framework from iOS it self (added by default).


@interface BNRLogger : NSObject    

Interface section, here you can write name of your class or category, in this example, we're creating a custom class named BNRLogger which derived from NSObject. You can read more about NSObject here and please also refer document for Class.


@property (nonatomic) NSDate *lastTime;

Properties section, here we write public properties which can be access outer of the class by creating object of the class. A property can be atomic/nonatomic and strong/weak/retain/assign/copy/readonly/readwrite as per the requirement. In this example, we added NSDate with variable name lastTime (as it should be a pointer so written as *lastTime). You can read What's the difference between the atomic and nonatomic attributes? and Objective-C ARC: strong vs retain and weak vs assign to get some idea on this.


-(NSString *)lastTimeString;
-(void)updateLastTime: (NSTimer *)t;    

Method define section, here we can add method definition which we will write inside <Class Name>.m

Here in first example, the first method will return a NSString, string is also a pointer type, we've written like -(NSString *)lastTimeString; in Objective-C each line should end with ; (a semi-colon). One can call this function within the same class using NSString *t = [self lastTimeString]; or from different class using object name of class.

In second example, method's return type is void and method name is updateLastTime which takes a NSTimer as argument, where t is a NSTimer variable, by which one can have timer within that function.

Reference, Method Syntax in Objective C


@end

End section, this should be the last line of any class, only protocol can be written out side of this. It tells to compiler that don't need to compile further.

Community
  • 1
  • 1
Hemang
  • 26,840
  • 19
  • 119
  • 186