Is it really strange or its just strange for me?!


#1

Hi, I’m student of second year Software Engineering and passed few courses on C++ , .Net using C# and VB, Java and also C.
After reading few chapters of Cocoa programming and programming in Objective-C Books I’ve gotten quit confused.
I’ve found that everything in Cocoa is defined and works based on delegate and predefined functions and methods. Which means programmer should go and find something which has been made by someone before for what he planned to do - - - e.g.
For every chapter of Cocoa Programming book there are lists of methods and function that book mostly focused on them

-(void)drawRect:(NSRect)rect
-(void)alertEnded:(NSAlert *)alert
-(id)objectForKey:(NSString *)defaultName
and so on, and so on, and so on...

I don’t know I can deliver what I mean or not, but other languages such as Java, functions mostly defined by the programmer and functions get called in other functions. Or in C# programming structure is mostly event based. Even I still haven’t realized how functions get called in another functions in Objective-C as all I saw in the books is how to apply predefined methods and there is method for whatever you want to do.
Please someone explain what is going on or I die young. :smiley:


#2

Found a part of my Answer. :stuck_out_tongue:
Header File :

#import <Foundation/Foundation.h>

@interface functionSource : NSObject
{
    int num1;
    int num2;
    NSString *str1;
    NSString *str2;
}

@property (retain) NSString *str1;
@property (retain) NSString *str2;

@property (assign) int num1;
@property (assign) int num2;

//-(void)setString1:(NSString*)x;
//-(NSString*)string1;
//
//-(void)setString2:(NSString*)x;
//-(NSString*)string2;
//
//-(void)setInteger1:(int)x;
//-(int)integer1;
//
//-(void)setInteger2:(int)x;
//-(int)integer2;

@end

Implementation File :

#import "functionSource.h"

@implementation functionSource
@synthesize str1;
@synthesize str2;
@synthesize num1;
@synthesize num2;

-(id)init
{
    self = [super init];
    if(self)
    {
        str1 = @"behnam izadi";
        NSLog(@"%@",str1);
    }
    return self;
}

//-(void)setString1:(NSString*)x
//{
//    str1 = x;
//}
//-(NSString*)string1
//{
//    return str1;
//}
//
//-(void)setString2:(NSString*)x
//{
//    str2 = x;
//}
//-(NSString*)string2
//{
//    return str2;
//}
//
//-(void)setInteger1:(int)x
//{
//    num1 = x;
//}
//-(int)integer1
//{
//    return num1;
//}
//
//-(void)setInteger2:(int)x;
//{
//    num2 = x;
//}
//-(int)integer2;
//{
//    return num2;
//}

@end

main.m

#import <Foundation/Foundation.h>

#import "functionSource.h"

int main (int argc, const char * argv[])
{

    @autoreleasepool {
        
        functionSource *fns = [[functionSource alloc]init];
//Using Properties
        fns.str2 = @"Amin Tabari";
        NSLog(@"%@",[fns str2]);
//Using Accessors
//      [fns setString2:@"Amin Tabari"];
//      NSLog(@"%@",[fns string2]);
        
    }
    return 0;
}

This was how to set and get values inter class
calling a function from another class works same way
once you alloc and init an instance of the class you can do that.
for calling function inside the class simply we use :
[self functionName];

yet I don’t know why we so much rely on predefined methods in Cocoa.


#3

I am not completely sure what your issue is here … I think at the core you are wondering why Cocoa offers so much functionality out of the box.

Cocoa is not a bit different from Java or .Net in that respect. All three of them offer exhaustive and excellent libraries. This is a GOOD thing. Yes, you should always look for ready-made solutions before implementing your own. No, re-inventing the wheel is not a good thing. In other words: the bigger the library the framework you are programming against offers, the better for you.

I know from personal experience that there are a lot of self-proclaimed ‘hackers’ out there that will try and want to make you believe that you should not rely on APIs and SDKs. The same guys will also sneer down at you for using an IDE (like Visual Studio or xCode) and claim that ‘real’ programmers only need the command line and a text editor. In my book, those people have lost contact with reality at some stage - given they ever were in contact with it in the first place.

Most programmers that actually produce useable results concentrate on solving extrinsic problems. That is, they write software that has the aim to make real life problems easier to tackle (or downright solve them). Without APIs and SDKs writing almost any type of software - no matter how small - would turn into a 10 man-year project.

Just one example: have a look at the incredibly well designed and useful System.Data namespace in .Net. With only a few lines of code, the classes and static functions in that namespace will let you do the full set of CRUD actions without ever having to think about what is going on in the background. It is good if you know how it works, but there is no need to.

You ARE GOING to write your own set of classes and reach an considerable degree of complexity in your libraries - trust me on that. So any pre-fabricated class and its methods is god-sent if you are programmer in the real world.


#4

@martini First of all I want to thank you for spending time and writing such a great complex answer,
by the time I wrote that question I was in a huge confusion which I believe everyone will get involved once they shift from languages like C# to Objective-C and cocoa framework, and mostly it happens because many people learning by comparison. I mean comparing of what you have learnt to what is new ahead.
By the time I writing this there is no more confusion in my mind since I read articles and pages of books. My suggestion to everyone try to migrate to Objective-C and Cocoa is start with a fresh mind in case of confusion. Logic of programming and the structure is same among every programming languages but syntax is vary and also the way of using libraries might be different. :slight_smile: