Written by Rinlapat Pu posted in Objective-C on Tuesday, April 24, 2012 @ 20:52:44

Code from Programming in Objective-C [part1]:

 

//

//  main.m

//  hello

//

//  Created by prair on 4/19/12.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

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

{

 

    @autoreleasepool {

        

        // insert code here...

        NSLog(@"Hello, World!");

        

    }

    return 0;

}

****************************************************************************************************************

#import <Foundation/Foundation.h>

#import is used to import information from a file,  a system file e.g. <Foundation/Foundation.h> or a file that you created e.g. "Fraction.h", into the program. The imported file's information, classes and functions, will be used later in the program.

So, this line of code means that to import or include the Foundation.h system file into the program.

***************************************************************************************************************

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

int is the type of value, an integer, that main returns.

main is used to indicate that where the program is to begin execution.

Arguments or parameters will be inside the parentheses.

return 0;

The main returns 0 if the program ended normally; otherwise it means that there are some problems occurred.

***************************************************************************************************************

@autoreleasepool { }

@autoreleasepool is a mechanism that allows the system to efficiently manage the memory your application uses as it creates new objects. So, all objects in it will be released when the end of block is reached or deallocated.

***************************************************************************************************************

NSLog(@"Hello, World!");

NSLog can be used to display its arguments or messages to the console.

***************************************************************************************************************

Written by Rinlapat Pu posted in Objective-C on Thursday, April 19, 2012 @ 06:59:26

You can write, compile, and run an Objective-C project on both terminal and Xcode application. In this case, I prefer to use Xcode.

First of all, you have to create a new project in Xcode.

ScreenShot1

 

Choose "Command Line Tool" and click "Next":

ScreenShot2

 

Specify the  project name (hello) and directory that the project folder will be stored

ScreenShot3

Finally, hello project will be created and after you've opened the main.m file, you will see the following lines of code.

//

//  main.m

//  hello

//

//  Created by prair on 4/19/12.

//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.

//

 

#import <Foundation/Foundation.h>

 

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

{

 

    @autoreleasepool {

        

        // insert code here...

        NSLog(@"Hello, World!");

        

    }

    return 0;

}

 

When you build and run the program, "Hello, World!" will be displayed on the console.

 

The seven comment lines at the top of the main.m file is used to document  a program and tell the reader of the program that who responds to maintain the program.

There are two ways to comment the code in Objective-C which are // and /* */

1. // -> Compiler will ignore the code or syntax located after two consecutive slash character (//) until the end of that line.

2. /* */ -> Compiler will ignore the code or syntax located between /* and */.

The following are three main benefits of using comments:

1. It's easy to explain your program logic using comments when your ideas are still fresh. You have no need to rethink about your logic later.

2. Helpful during debug phrase. Inserting comments to an early stage of the program will prevent program logic errors.

3. Reduce time to work on a document.

 

The meaning of each syntax in the main.m file will be explained in the next post, Programming in Objective-C [part2].

Written by Rinlapat Pu posted in Command Line on Tuesday, April 3, 2012 @ 23:08:44

Open the terminal and type "cancel -a" command to clear entire print queue:


$ cancel -a

Written by Rinlapat Pu posted in Ruby on Rails on Friday, January 27, 2012 @ 02:57:41

First of all, you have to install jeweler gem.

$ sudo gem install jeweler

Next, create a new project (project name must be lowercase letters)

$ jeweler firstgem

After that, it will generate several files and directories.

$ cd firstgem

$ bundle install

$ ls

#=> Gemfile  Gemfile.lock  lib  LICENSE.txt  Rakefile  README.rdoc  test

Don't forget to configure the description and dependencies in Rakefile.

Finally, let's write code and set the version to your gem.

$ rake version:write
#=> Updated version: 0.0.0 (if it is the first version)

 

Copyright Khun9IT © 2011