[Tutorials] Objective-C & Cocoa Mac Application Development

Everything related to programming.
Sparky
Delta Force
Posts: 4194
Joined: Wed Mar 31, 2004 8:59 pm
Location: New Jersey, USA
Contact:

[Tutorials] Objective-C & Cocoa Mac Application Development

Post by Sparky » Mon Oct 10, 2011 12:51 pm

I'm so excited we have this programming forum! Let's have some "small talk" about the main programming languages for distributable Mac platform applications, Objective-C and Cocoa.

There are a few references that I've been using to learn these languages.
  • Programming in Objective-C 2.0 Second Edition, by Stephen G. Kochan runs through the topic and touches on all the major points. The writing style is understandable, and this information is worthy of publication in a book. This book works well as a reference also.
  • Learning Cocoa with Objective-C by the O'Reilly Commons I think is the best second step to learning how to develop Mac applications. The presentation clarifies fundamental principles that have not fully set in the reader's mind after having read Kochan's book.
  • Cocoa Programming for Mac OS X Third Edition, by Aaron Hillegass does a fine job covering a variety of Cocoa development tasks. It even has a small section on OpenGL. However, each section is of limited use, since each section only uses that information with a single tutorial that requires you to read through the book as you develop that application. Code is explained, but this should be read after the O'Reilly Commons is used. This book works well as a reference for many common aspects of Cocoa.
I've also been studying OpenGL, which I think is essential to learn when programming graphical applications like games, since you would be working with topics like model importing and exporting, lightmaps, buffering, rendering and environment physics and navigation.

There are two standard OpenGL books that I'm using, the Reference and the Tutorial that are available to buy (or check your local library branch). You should learn Cocoa before you add OpenGL to your tool belt. Write some simple programs and glance over some samples from the Apple Developer Library whose online pages are accessibly linked through XCode. They can help at least with understanding fullscreen mode. Do a web search for opengl OBJ model import tutorials and you'll learn how to use the two GLUT frameworks to handle .obj models so that you can import and export models into your game or modding tool.

No further advice at this time, aside from pacing yourself.
Either you are groping for answers, or you are asking God and listening to Jesus.

nil
Halo Moderator
Halo Moderator
Posts: 1090
Joined: Sat Jul 05, 2008 8:38 am
Location: null zone

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by nil » Mon Oct 10, 2011 1:42 pm

I'm just going to say that Cocoa has a high difficulty curve and is not something I at all recommend for a beginner programmer. Nor do I recommend doing anything GUI-like as a beginner, either.

If you want to learn opengl, what you will need is to learn C, not Cocoa. GLUT isn't really good for real use, but it's great for beginners to use, so use it if you want to learn OpenGL. There are other libraries that don't require you to directly interface with Cocoa as well.
Sparky wrote:I've also been studying OpenGL, which I think is essential to learn when programming graphical applications like games, since you would be working with topics like model importing and exporting, lightmaps, buffering, rendering and environment physics and navigation.
If you want to program games, your first game will likely be something simple (but much harder to program than you may possibly imagine) like pong -- all these topics Sparky mentions (lightmaps, mode importing) will be the least of your worries and will not matter whatsoever to anyone starting out.
I am no longer active to Halo or MGM, and don't guarantee a response on the forums or through email. I will however linger around the discord room for general chatting. It's been fun!

Sparky
Delta Force
Posts: 4194
Joined: Wed Mar 31, 2004 8:59 pm
Location: New Jersey, USA
Contact:

Notes from the Cocoa / Objective-C overview wiki

Post by Sparky » Wed Nov 09, 2011 2:27 pm

Here are my notes as I reviewed the fundamental concepts of Object-Oriented Programming again using sections 1 to 4 of this very helpful guide: http://commons.oreilly.com/wiki/index.p ... bjective-C.

I suggest you copy-paste this information into a text program that uses syntax coloring, but let me just say that these notes are best viewed on a 1920x1080 resolution monitor or in a program that has line wrapping turned off.

I might continue these notes for the additional sections of the overview of the topic, but I stopped because the fifth section gets into using XCode.

Code: Select all

// Abstract of Object Oriented Programming using "objective"-c

DEFINITION		Objects are modular units of operations and data. Together, they form structured networks. The primary elements of Object-oriented program design are the design of objects and their interactions.
VALUE			Isolate modular units so that changes to one part of the program do not adversely affect its other parts. Functionalities (frameworks) become reusable in other programs also.
DEFINITION		A class is an object that defines a single type of object, like a blueprint for creating each instance of a single kind of object. Each kind of object interacts the same with other objects.
VALUE			One type of defined interaction per (object) class. Class defines structure (and memory use) within each object; each instance of the same type of object exposes the same interactivity to other objects.
DEFINITION		An instance gets its own memory allocation so it can have its own state and lead an independent existence. The class which is the blueprint of that instance specifies the amount of memory used.
DEFINITION		Since a class is a first-class object, it can have methods also. Methods associated with classes are called class methods. Each object (and class object) has a reference to its own class.
DEFINITION		Classes can be defined as specializations of other classes ("this new object is a kind of this older object"), meaning inheritance. Objects can also declare that they obey protocols that define behaviors.
VALUE			Inheritance is the ability to collect similar functionalities of different classes and group them into a common parent class. Define classes that are common and unique. Classes are like definitions, objects can be similes.
DEFINITION		"A [subclass] is a type of [superclass]." Subclasses inherit state (in the form of variable definitions) and functionality from the superclass, but they can have very different underlying implementations.
DEFINITION		Object composition differs from inheritance. It means using a single class to create a set of classes, and can make programs more stable than inheritance.

// Example of creating an object

int main (int argc, const char * argv[])
{
	NSAutoReleasepool *pool = [[NSAutoreleasepool alloc] init];		// The code here for the alloc Class Method on NSAutoreleasepool and init Instance Method on pool is formed in shorthand for the coding approach used for the next three lines; this line begins management of this application's memory by using the memory handling capabilities of the NSAutoReleasepool class.
	
	NSObject * object;			// A regular C pointer to a new variable (a new object) named "object" that is defined to be an object of the class NSObject.
	object = [NSObject alloc];	// Allocates and reserves memory for the object variable; also returns a pointer to that memory address.
	object = [object init];		// The next required step after allocating memory to an object is to initialize it. 
								// Alone, the init method might return the wrong object, so you specify our object as the receiver so that this object can now be used.
	NSLog(@"Created object: %@", object);		// prints to the console. the format string token %@ is used to print the variable as an object, rather than a number or string. The result is a display of the variable's memory address.
	
	[pool release];			// Subtract 1 from the memory counter for the NSAutoReleasepool *pool object, going from 1 to 0.
	return 0;		// Return no values, indicating a normal end to the program (no information given, or NULL).
}

// More definitions and examples

DEFINITION		Methods are structured like C functions and are procedures associated with, and implemented by, the object's class. Methods can either be used by classes or by object instances. (Class Methods and Instance Methods.)
				Methods can only be used by objects, not by primitive types like numbers.
EXPLANATION		To use (to "call" -- a phone analogy) a method, send an object a message telling it to apply that method. Square brackets are used with messages (just like arrays!).
EXPRESSION		ReturnedValue 	= [Receiver 	MethodName];
EXAMPLE			anObject		= [NSObject		alloc];
EXAMINATION		The message is the expression enclosed in square brackets. Variable (anObject), Assignment Operator (=), Message ([NSObject alloc]). The message should contain the class object or instance object and the method to send it. The variable subsequently becomes a shorthand for sending the object that message, and in this example, the NSObject class returns a new instance of that class which will then be assigned to the variable anObject.

DEFINITION		Within a message, there can be several arguments given to the method. Every message argument is identified with a label (a colon-terminated keyword). A method's arguments are considered part of the method name.
EXPLANATION		A colon terminates method names that take an argument; methods without arguments do not have colons.
EXPRESSION		ReturnedValue	= [Receiver			MethodName:		Argument];
EXAMPLE			var 			= [rectangle 		setWidth:		width];
EXAMINATION		When var is used, its message tells the runtime to call the "setWidth:" method used by the rectangle class or instance.

EXPLANATION		You can send multiple arguments to a method from within a message. This employs Smalltalk syntax.
EXPRESSION		ReturnedValue	= [Receiver			First part of method name		Argument 1			Second part of method name		Argument 2];
EXAMPLE			var				= [rectangle		setWidth:						width				height:							height];
EXAMINATION		In the above example, setWidth:height: refers to one method, not two; that is the name of the method. It calls a method with two arguments; you must pass in two arguments. This syntax can aid in code legibility.

EXPLANATION		You can nest messages within each other. Remember that messages can only be used with objects, or else the program will crash. This lets you reuse a returned value as an argument without declaring a variable for it.
EXPRESSION		object			= [OuterMessage 	[InnerMessage] 		OuterMessage];
EXAMPLE			object			= [					[NSObject alloc]	init];
EXAMINATION		The inner expression is evaluated first, and must return an object (that is, the memory address of an object). That returned object becomes the receiver of the second message when the next nested expression is evaluated.

// Objective-C-defined types
Type		Representation						Explanation
id			An object reference					A pointer to an object's data structure; can be used to type any kind of object, class, or instance
Class		A class object reference			A pointer to a class's data structure
SEL			Selector							A compiler-assigned code that identifies a method name from a method lookup table (array)
IMP			Method Implementation				A pointer to a method implementation that returns an id
BOOL		Boolean value						YES or NO
nil			(id)0								A null OBJECT pointer
Nil			(Class)0							A null CLASS pointer
Additionally, you can type static instances of a class by using the class name as a type name. This is a pointer to an instance of its class or to an instance of any class from which it inherits.
Either you are groping for answers, or you are asking God and listening to Jesus.

Fonzeh
Ranger
Posts: 1894
Joined: Tue Oct 16, 2007 3:57 am
Location: "I didn't just take your mom out to dinner. I ate your mom for dinner."
Contact:

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by Fonzeh » Sun Nov 13, 2011 10:36 pm

next time sparky make your comments more end friendly. I have a widescreen monitor and still had to scroll left / right to read.

Put inside Notepad++ because it had highlighting.

BUT A GODAMN AWESOME EXPLANATION OF THE TERMS AND ETC USED IN OBJ-C. YOU SIR ARE AWESOME.

just could've been cleaned a bit, I don't plan on using obj - c until i get a Macintosh come January, but when i do obj-c is more or less going to be mine main gramming language. right now im learning C++ and already am fucking it on a regular.
G[v]N wrote:HUGE NOTIFICATION
THIS GRAVY HAS BRAINS
Mota-Lev was here 30/4/2010@2:18pm
Image

Sparky
Delta Force
Posts: 4194
Joined: Wed Mar 31, 2004 8:59 pm
Location: New Jersey, USA
Contact:

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by Sparky » Tue Nov 15, 2011 7:42 pm

To continue on to section 5 of the tutorial, here is my "Song" class header file, Song.h:

Code: Select all

//
//  Song.h      -- define the Song class, including within the class its variables, and including outside the class its methods on how to use those variables
//  songs
//
//  Created by Sparky on 11/15/11.
//  Copyright 2011 MacGamingMods.com. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface Song : NSObject {    // blueprint for "Song" instances
@private

// all the instance variables used by this class go inside these braces
    
    NSString * name;    // this variable "name" points to a string object
    NSString * artist;  // this variable "artist" points to another string object
    
}

// all the methods of the class are declared here

- (NSString *)name;     // Get the name of the song (read) -- declares an instance method called "name" that returns the pointer to a string object when this method is called
- (void)setName:(NSString *)newName;    // Change the name of the song (write) -- declares an instance method called "setName:" that takes a pointer to a string object as its argument. this instance method sets the name of the song that our object represents. it is a void because it returns nothing (it's write-only).

- (NSString *)artist;   // (same concepts as the previous two instance method declarations)
- (void)setArtist:(NSString *)newArtist;

// the above methods are called accessor methods, and they access the variables which are known as properties, and return those variables to the caller of the method

@end
And here is the related Song.m file (in progress, as I continue the tutorial):

Code: Select all

//
//  Song.m
//  songs
//
//  Created by Sparky on 11/15/11.
//  Copyright 2011 MacGamingMods.com. All rights reserved.
//

#import "Song.h"    // using the previously-defined definitions and declarations (the "interface") for our Song class


@implementation Song

// generic code...

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

// ...end of generic code.
// Our classes...

- (NSString *)name      // our instance method for reading the name object/variable...
{
    return name;        // ...which returns the NSString object associated with the name variable as a string... as it should, because we want song names to be of the string type.
}

- (void)setName:(NSString *)newName     // here is the setName: method that will write new names
{
    [newName retain];       // a bit of memory management, saying to the object newName that we intend to keep a reference to it('s memory values)...
    [name release];         // ...but we are no longer interested in any potentially existing values of the name object, because we want to change it. we send name a message that we don't need it here anymore.
    name = newName;         // and here we change the name object so that it is the value of the new name
}

- (NSString *)artist    // the same principles apply as above
{
    return artist;
}

- (void)setArtist:(NSString *)newArtist
{
    [newArtist retain];
    [artist release];
    artist = newArtist;
}

- (NSString *)description   // here is an example of polymorphism, where we want to handle information used by the "description" method in a different way than how it is already used as it is defined in the NSObject class.
{                           // we are overriding the existing use of "description" with this new definition; "description" is already defined within the NSObject class from which Song inherits as a subclass.
    return [self name];     // return the name of the song as its description; "self" refers to the object under operation, and if the internal implementation of the Song class changes, this method will continue to work.
}

@end    // compiler directive saying the implementation ends here
And here is the main.m file, which actually makes things happen:

Code: Select all

//
//  main.m      -- the place where things actually happen
//  songs
//
//  Created by Sparky on 11/15/11.
//  Copyright 2011 MacGamingMods.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Song.h"    // we want to use the Song class, so we need access to its interface file

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Song * song1 = [[Song alloc] init];     // allocate and initialize song1 as an object instance of type Song (read as, "class Song points to a new instance called song1; allocate memory for it, then initialize it") (...)
    [song1 setName:@"We Have Explosive"];   // write the name of song1 to its memory
    [song1 setArtist:@"The Future Sound of London"];    // write the artist of song1 to its memory
    
    Song * song2 = [[Song alloc] init];     // do it again with another instance called song2. same difference.
    [song2 setName:@"Loops of Fury"];
    [song2 setArtist:@"The Chemical Brothers"];
    
    NSLog(@"Song 1: %@", song1);    // let's see the results of those objects, but this will only display memory pointer locations so far... like "Song 1: <Song: 0x100108d80>"
    NSLog(@"Song 2: %@", song2);    // ...and "Song 2: <Song: 0x10010c990>", until we override the "description" method in our Song.m file.
    
    [pool drain];
    return 0;
}

Either you are groping for answers, or you are asking God and listening to Jesus.

nil
Halo Moderator
Halo Moderator
Posts: 1090
Joined: Sat Jul 05, 2008 8:38 am
Location: null zone

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by nil » Wed Nov 16, 2011 8:21 pm

Fonzie wrote:next time sparky make your comments more end friendly. I have a widescreen monitor and still had to scroll left / right to read.

Put inside Notepad++ because it had highlighting.

BUT A GODAMN AWESOME EXPLANATION OF THE TERMS AND ETC USED IN OBJ-C. YOU SIR ARE AWESOME.
He could be using a decent coding editor with syntax highlighting for all you know. You could just copy and paste the code into your favorite coding editor of choice. Nothing is stopping you from doing that.

When you show code to people, there are two sane options:
a) Use a pasting website designed for pasting code (eg: http://pastebin.com)
b) Complain that the forum itself doesn't have syntax highlighting for pasting code in code tags.

The insane non-existent option is uploading a screenshot of your text editor ;). (Obvious reasons, makes code not copy-paste friendly).

..Anyway, Sparky. If you don't know already, take a look at Obj-C properties. They basically remove the necessity to write all the code you did for accessors and setters. That'd seem to be a nice next tutorial - converting your current code for Song class to use properties for name and artist. Also, @private is kind of heavily implied in Obj-C as the default access modifier - protected and public are rarely ever even used in my experience and obj-c doesn't care about access modifiers that much anyway, so it's not necessary to specify @private on the top.

[edit]: I'm wrong about private being default accessor, protected actually is the default, but these are still rarely used anyway because using accessors & setters are preferred.
Last edited by nil on Thu Nov 17, 2011 7:19 pm, edited 2 times in total.
I am no longer active to Halo or MGM, and don't guarantee a response on the forums or through email. I will however linger around the discord room for general chatting. It's been fun!

Sparky
Delta Force
Posts: 4194
Joined: Wed Mar 31, 2004 8:59 pm
Location: New Jersey, USA
Contact:

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by Sparky » Thu Nov 17, 2011 11:43 am

Are you referring to my Zeus development or this application whose code was explicated in the tutorial I'm using?

PasteBin will be my next attempt. In fact, I'll post the Zeus Map File Parser header file in the Zeus topic. Please look there to discuss what I'm doing.
Either you are groping for answers, or you are asking God and listening to Jesus.

nil
Halo Moderator
Halo Moderator
Posts: 1090
Joined: Sat Jul 05, 2008 8:38 am
Location: null zone

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by nil » Thu Nov 17, 2011 11:58 am

nil wrote:...They basically remove the necessity to write all the code you did for accessors and setters. That'd seem to be a nice next tutorial - converting your current code for Song class to use properties for name and artist...
Which do you think?

Since I never mentioned Zeus, why did you?
I am no longer active to Halo or MGM, and don't guarantee a response on the forums or through email. I will however linger around the discord room for general chatting. It's been fun!

Sparky
Delta Force
Posts: 4194
Joined: Wed Mar 31, 2004 8:59 pm
Location: New Jersey, USA
Contact:

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by Sparky » Thu Nov 17, 2011 7:02 pm

nil, I suggest reading through the tutorial if you have not already. Tomorrow, I plan to work with the Document-based application tutorial chapters while coding that application you didn't mention here.
Either you are groping for answers, or you are asking God and listening to Jesus.

nil
Halo Moderator
Halo Moderator
Posts: 1090
Joined: Sat Jul 05, 2008 8:38 am
Location: null zone

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by nil » Thu Nov 17, 2011 7:19 pm

Sparky wrote:nil, I suggest reading through the tutorial if you have not already. Tomorrow, I plan to work with the Document-based application tutorial chapters while coding that application you didn't mention here.
I don't understand you.
I am no longer active to Halo or MGM, and don't guarantee a response on the forums or through email. I will however linger around the discord room for general chatting. It's been fun!

Fonzeh
Ranger
Posts: 1894
Joined: Tue Oct 16, 2007 3:57 am
Location: "I didn't just take your mom out to dinner. I ate your mom for dinner."
Contact:

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by Fonzeh » Fri Nov 18, 2011 2:08 am

I was lost at both of you. Both you need to quit
G[v]N wrote:HUGE NOTIFICATION
THIS GRAVY HAS BRAINS
Mota-Lev was here 30/4/2010@2:18pm
Image

Sparky
Delta Force
Posts: 4194
Joined: Wed Mar 31, 2004 8:59 pm
Location: New Jersey, USA
Contact:

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by Sparky » Fri Nov 18, 2011 3:53 am

Either you are groping for answers, or you are asking God and listening to Jesus.

nil
Halo Moderator
Halo Moderator
Posts: 1090
Joined: Sat Jul 05, 2008 8:38 am
Location: null zone

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by nil » Fri Nov 18, 2011 8:03 am

nil wrote:If you don't know already, take a look at Obj-C properties. They basically remove the necessity to write all the code you did for accessors and setters.
Nothing Sparky posted convinces me he has the slightest clue of what I am talking about. He makes no sense. I'm sure anyone who understood my post (that is, familiar enough with Obj-C or trying to learn it), will agree with me.

@Fonzie: Saying you aren't following what is going on and you both need to quit does not sound too convincing.
I am no longer active to Halo or MGM, and don't guarantee a response on the forums or through email. I will however linger around the discord room for general chatting. It's been fun!

Excend
Commando
Posts: 2791
Joined: Sun May 06, 2007 3:27 am
Location: SiK x Gh0sTs Rehauled.
Contact:

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by Excend » Fri Nov 18, 2011 8:19 am

What Nil is saying is that he made this point:
nil wrote: ..Anyway, Sparky. If you don't know already, take a look at Obj-C properties. They basically remove the necessity to write all the code you did for accessors and setters. That'd seem to be a nice next tutorial - converting your current code for Song class to use properties for name and artist.
To which Sparky gave no real response.

Now my understanding of where the confusion comes in: Sparky asked if Nil was referring to the tutorial itself (yes he was) or the development of Zeus. Nil questioned why Zeus was brought into it, but Sparky is implementing the songs into Zeus so development of his method corresponds with development of Zeus. At this point I think that Sparky thinks Nil is looking for support, while Nil is trying to help Sparky. So, Sparky, please read my quote from Nil as it is a suggestion from him to you to help with your code and to make your tutorials better.
TaxiService wrote:Roses are red
Violets are blue
What a shitty thread
Fuck all of you.

Dirk Gently
Commando
Posts: 2047
Joined: Sun Oct 21, 2007 2:34 pm
Location: 3C0E9056
Contact:

Re: [Tutorials] Objective-C & Cocoa Mac Application Developm

Post by Dirk Gently » Fri Nov 18, 2011 8:58 am

What nil is referring to is how Objective-C 2.0 "style" is done. This is the new standard of writing in Objective-C because it completely eliminates the need to write your own implementations of accessing members of an object. That alone can drastically cut out a lot of code you have to write.

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests