Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

A m a n y ~

macrumors newbie
Original poster
Jun 9, 2009
9
0
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


PHP:
#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:
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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.
http://developer.apple.com/document....html#//apple_ref/doc/uid/TP40001418-CH3g-SW4

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

-Lee

Edit:
Here is some code that uses more objective-C to achieve what your code is doing with C functions:
main.m:
Code:
#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;
}
intComparer.h:
Code:
#import <Cocoa/Cocoa.h>

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

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

- (BOOL) areEqual;

- (BOOL) areNotEqual;

- (NSString *) getDescription;

@end
intComparer.m:
Code:
#import "intComparer.h"

@implementation intComparer
  -initWith: (int) x and: (int) y
  {
	self = [super init];
	if (self) {
		compareOne=x;
		compareTwo=y;
	}
	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 "];
  }
  
@end
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.