View Full Version : I have some question about objective-c

A m a n y ~
Jul 8, 2009, 08:46 PM
Hi every one here
I am glad to be here I and to found like these forum


I start to learn objective-c ..
but in beginning I can'nt realize some thing ..
exactly in these program

#import <Foundation/Foundation.h>
// returns NO if the two integers have the same
// value, YES otherwise
BOOL areIntsDifferent (int thing1, int thing2)
if (thing1 == thing2) {
return (NO);
} else {
return (YES);
} // areIntsDifferent
// given a YES value, return the human- readable
// string "YES". Otherwise return "NO"
NSString *boolString (BOOL yesNo)
if (yesNo == NO) {
return (@"NO");
} else {
return (@"YES");
} // boolString
int main (int argc, const char *argv[])
BOOL areTheyDifferent;
areTheyDifferent = areIntsDifferent (5, 5);
NSLog (@"are %d and %d different? %@",
5, 5, boolString(areTheyDifferent));
areTheyDifferent = areIntsDifferent (23, 42);
NSLog (@"are %d and %d different? %@",
23, 42, boolString(areTheyDifferent));
return (0);
} // main

1- in c program we write the function after main function , just we delectation it before main , is [BOOL areIntsDifferent (int thing1, int thing2)]
function , it does mean we can write it before main .

2- [NSString *boolString (BOOL yesNo)] , what does it mean the star * before [*boolString] also here [int main (int argc, const char *argv[])]

3- in main .. [ BOOL areTheyDifferent; ]
is these a delectation of variable it's type BOOL {yes,NO}
and it's name is areTheyDifferent ,,, or what ? and what it do in the main (what is benefit here in the main )

sorry for my questions :confused:

thank u at all , good luck :cool:

Jul 8, 2009, 09:09 PM
This is a bit difficult to answer, because you haven't written any class or instance methods related to an objective-c class. Only when you're passing a message to an object or class do you use the [x y:z] syntax. You've written regular C functions. You certainly could write a prototype before main like any other C program, and write your functions after main.

The only "non-C" thing you've done is use some NSStrings, which are Objective-C objects. NSString * just means a pointer to an NSString. Every object in Objective-C is on the heap, and you only store local pointers to them. You cannot have a local object.

areTheyDifferent is just a stack local variable in main of type BOOL. BOOL is just typedef'd to an unsigned char, and YES and NO are simply #defined to 1 and 0 respectively.

The benefit of using this variable to temporarily hold the result of the comparison is just for clarity. You could nest everything:
NSLog(@"are %d and %d different? %@",5,5, boolString(areIntsDifferent(5,5)));
it's just a little sloppy.


Here is some code that uses more objective-C to achieve what your code is doing with C functions:

#import <Cocoa/Cocoa.h>
#import "intComparer.h"

int main(int argc, char *argv[])
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
intComparer *myIC = [[intComparer alloc] initWith:5 and:5];
NSLog(@"%@",[myIC getDescription]);
[myIC release];
myIC = [[intComparer alloc] initWith: 42 and: 44];
NSLog(@"%@",[myIC getDescription]);
[myIC release];
[pool drain];
return 0;


#import <Cocoa/Cocoa.h>

@interface intComparer : NSObject {
int compareOne;
int compareTwo;

- initWith: (int) x and: (int) y;

- (BOOL) areEqual;

- (BOOL) areNotEqual;

- (NSString *) getDescription;



#import "intComparer.h"

@implementation intComparer
-initWith: (int) x and: (int) y
self = [super init];
if (self) {
return self;

-(BOOL) areEqual
return compareOne==compareTwo?YES:NO;

-(BOOL) areNotEqual
return compareOne!=compareTwo?YES:NO;

-(NSString *) getDescription
return [NSString stringWithFormat:@"%d and %d are %@equal",compareOne,compareTwo,[self areEqual]?@"":@"not "];


A m a n y ~
Jul 11, 2009, 02:18 PM
Thank u sir alot :)
really now it is understood
more thanx ... ;)