Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 8, 2009, 08:46 PM   #1
A m a n y ~
macrumors newbie
 
Join Date: Jun 2009
I have some question about objective-c

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 Code:
#import <Foundation/Foundation.h>
// returns NO if the two integers have the same
// value, YES otherwise
BOOL areIntsDifferent (int thing1int 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 (55);
NSLog (@"are %d and %d different? %@",
55boolString(areTheyDifferent));
areTheyDifferent areIntsDifferent (2342);
NSLog (@"are %d and %d different? %@",
2342boolString(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

thank u at all , good luck

Last edited by A m a n y ~; Jul 8, 2009 at 08:51 PM.
A m a n y ~ is offline   0 Reply With Quote
Old Jul 8, 2009, 09:09 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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/documenta...01418-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

Last edited by lee1210; Jul 8, 2009 at 09:36 PM.
lee1210 is offline   0 Reply With Quote
Old Jul 11, 2009, 02:18 PM   #3
A m a n y ~
Thread Starter
macrumors newbie
 
Join Date: Jun 2009
Thank u sir alot
really now it is understood
more thanx ...
A m a n y ~ is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Noob Objective-C / Framework / AVFoundation question Golgafrincham Mac Programming 0 Jan 30, 2014 02:54 AM
New Objective-C Tutorial ryanhodson Wasteland 4 Feb 1, 2013 01:24 AM
Objective-C / Java Question Mvkoe Mac Programming 3 Dec 8, 2012 08:27 AM
Objective C question farmerdoug Mac Programming 4 Sep 16, 2012 02:44 PM
Problems with Objective-C++? frank100 Mac Programming 5 Aug 8, 2012 04:49 PM

Forum Jump

All times are GMT -5. The time now is 07:39 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC