Objective-C Tutorial with Bucky

Cocoa.h:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <CoreData/CoreData.h>

t1: Setting up Xcode

Foundation Framework Reference

The Foundation framework defines a base layer of Objective-C classes. In addition to providing a set of useful primitive object classes, it introduces several paradigms that define functionality not covered by the Objective-C language. The Foundation framework is designed with these goals in mind:

1. Provides a small set of basic utility classes.
2. Make software development easier by introducing consistent conventions for things such as deallocation.
3. Support Unicode strings, object persistence, and object distribution.
4. Provides a level of OS independence, to enhance portability.

The Foundation framework includes the root object class, classes representing basic data types such as strings and byte arrays, collection classes for storing other objects, classes representing system information such as dates, and classes representing communication ports.

#import <Foundation/Foundation.h>
#include <Foundation/Foundation.h>

I got a template makefile for objective-c program.

GNUmakefile

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t1
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t1.m
OBJ = t1.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t1.o: t1.m

#Student.o: Person.h Student.h Student.m
#Teacher.o: Person.h Teacher.h Teacher.m
#main.o: Student.h Teacher.h

t1.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  /* Logs an error message to the Apple System Log facility */
  NSLog(@"Hello, Objective-C World.\n");

  /* There is no need to use #include <stdio.h> */
  /* START: C Source Code is Okay */
  int i, j;
  for(i = 1; i <= 5; i++){
    for(j = 0; j < i; j++){
      printf("*");
    }
    printf("\n");
  }
  printf("Hello, Objective-C World.\n");
  printf( "%e\n", sin(10) );
  /* END: C Source Code is Okay */

  [pool drain];
  
  return 0;
}

t1.m の実行結果は:

[wtopia t1]$ make
gcc -c -g -Wall t1.m
gcc -g -Wall -o t1 t1.o -framework Foundation
[wtopia t1]$ ./t1
2011-02-23 19:21:57.104 t1[30827:903] Hello, Objective-C World.
*
**
***
****
*****
Hello, Objective-C World.
-5.440211e-01

t2: Explaining the Program

In objective-c source code, we use #import as usual, but if we use #include is okay.

t3: Variables

t3.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int n1 = 20;
  int n2 = 40;
  int sum = n1 + n2;

  NSLog(@"my favorite num are %i and %i", n1, n2);
  NSLog(@"the sum of %i and %i is %i", n1, n2, sum);

  [pool drain];

  return 0;
}

t3.m の実行結果は:

[wtopia t3]$ ./t3
2011-02-23 19:35:21.566 t3[31035:903] my favorite num are 20 and 40
2011-02-23 19:35:21.569 t3[31035:903] the sum of 20 and 40 is 60

t4: Interface

t4.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#import <Foundation/Foundation.h>

// interface
@interface Person: NSObject{
  int age;
  int weight;
}

// method
- (void) print;
- (void) setAge: (int) a;
- (void) setWeight: (int) w;
@end

// implementation
@implementation Person

- (void) print{
  NSLog(@"I am %i years old and weight %i pounds", age, weight);
}

- (void) setAge: (int) a{
  age = a;
}

- (void) setWeight: (int) w{
  weight = w;
}

@end

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  
  // initializing a class
  Person *fei;
  fei = [Person alloc];
  fei = [fei init];

  // creating an Object
  [fei setAge: 28];
  [fei setWeight: 165];
  [fei print];
  [fei release];

  [pool drain];

  return 0;
}

t4.m の実行結果は:

[wtopia t4]$ ./t4
2011-02-23 19:55:14.352 t4[31143:903] I am 28 years old and weight 165 pounds

t5: Implementation

t6: Creating an Object

t7: Creating Multiple Objects

t7.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#import <Foundation/Foundation.h>

// interface
@interface Person: NSObject{
  int age;
  int weight;
}

// method
- (void) print;
- (void) setAge: (int) a;
- (void) setWeight: (int) w;
@end

// implementation
@implementation Person

- (void) print{
  NSLog(@"I am %i years old and weight %i pounds", age, weight);
}

- (void) setAge: (int) a{
  age = a;
}

- (void) setWeight: (int) w{
  weight = w;
}

@end

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Person *fei = [[Person alloc] init];
  Person *julia = [[Person alloc] init];

  // creating fei Object
  [fei setAge: 28];
  [fei setWeight: 165];
  [fei print];
  [fei release];

  // creating julia Object
  [julia setAge: 20];
  [julia setWeight: 100];
  [julia print];
  [julia release];

  [pool drain];

  return 0;
}

t7.m の実行結果は:

[wtopia t7]$ ./t7
2011-02-23 20:05:17.165 t7[31222:903] I am 28 years old and weight 165 pounds
2011-02-23 20:05:17.191 t7[31222:903] I am 20 years old and weight 100 pounds

t8: Accessing Instance Variabls

t8.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#import <Foundation/Foundation.h>

// interface
@interface Person: NSObject{
  int age;
  int weight;
}

// method
- (void) print;
- (void) setAge: (int) a;
- (void) setWeight: (int) w;
- (int) age;
- (int) weight;
@end

// implementation
@implementation Person

- (void) print{
  NSLog(@"I am %i years old and weight %i pounds", age, weight);
}

- (void) setAge: (int) a{
  age = a;
}

- (void) setWeight: (int) w{
  weight = w;
}

- (int) age{
  return age;
}

- (int) weight{
  return weight;
}

@end

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Person *fei = [[Person alloc] init];

  // creating fei Object
  [fei setAge: 28];
  [fei setWeight: 165];
  NSLog(@"fei is %i years and %i pounds", [fei age], [fei weight]);
  
  [pool drain];

  return 0;
}

t8.m の実行結果は:

[wtopia t8]$ ./t8
2011-02-23 21:59:49.642 t8[31391:903] fei is 28 years and 165 pounds

t9: Data Types and Other Stuff

t9.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int i = 20;
  float f = 3.14;
  double d = 2.134e+14;
  char c = 'a';
  int sum = 3 + 2*4;
  int m = 99 % 2;

  NSLog(@"%i", i);
  NSLog(@"%f", f);
  NSLog(@"%e", d);
  NSLog(@"%c", c);
  NSLog(@"%i", sum);
  NSLog(@"%i", m);

  [pool drain];
  
  return 0;
}

t9.m の実行結果は:

[wtopia t9]$ ./t9
2011-02-23 22:08:36.689 t9[31436:903] 20
2011-02-23 22:08:36.691 t9[31436:903] 3.140000
2011-02-23 22:08:36.692 t9[31436:903] 2.134000e+14
2011-02-23 22:08:36.692 t9[31436:903] a
2011-02-23 22:08:36.692 t9[31436:903] 11
2011-02-23 22:08:36.693 t9[31436:903] 1

t10: int and float Conversions

t10.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  float f1 = 29.123;
  float f2 = .0;

  int i1;
  int i2 = 90;
  int i3 = 33;

  i1 = f1;
  NSLog(@"%i", i1);

  f2 = (float)i2/(float)i3;
  NSLog(@"%f", f2);

  [pool drain];

  return 0;
}

t10.m の実行結果は:

[wtopia t10]$ ./t10
2011-02-23 22:16:59.856 t10[31547:903] 29
2011-02-23 22:16:59.858 t10[31547:903] 2.727273

t11: Type casting and Assignment Operators

t11.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  float f1 = 20.86;
  float f2;

  int i1;
  int i2 = 15;
  int i3 = 10;

  NSLog(@"%i", i1);
  NSLog(@"%d", i1);
  
  NSLog(@"%i", f1);
  NSLog(@"%f", f1);

  f2 = (float)i2 / (float)i3;
  NSLog(@"%f", f2);

  int feifei = (int)26.77;
  NSLog(@"%i", feifei);

  int times = 2*3;
  NSLog(@"%i", times);

  [pool drain];

  return 0;
}

t11.m の実行結果は:

[wtopia t11]$ ./t11
2011-02-23 22:26:28.389 t11[31749:903] 0
2011-02-23 22:26:28.392 t11[31749:903] 0
2011-02-23 22:26:28.393 t11[31749:903] 8550400
2011-02-23 22:26:28.394 t11[31749:903] 20.860001
2011-02-23 22:26:28.394 t11[31749:903] 1.500000
2011-02-23 22:26:28.395 t11[31749:903] 26
2011-02-23 22:26:28.396 t11[31749:903] 6

t12: Intro to Looping

t12.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int i;
  int sum = 0;

  for(i = 1; i <= 10; i++){
    sum += i;
    NSLog(@"%i", i);
  }
  NSLog(@"%i", sum);

  [pool drain];

  return 0;
}

t12.m の実行結果は:

[wtopia t12]$ ./t12
2011-02-23 22:31:23.054 t12[31786:903] 1
2011-02-23 22:31:23.058 t12[31786:903] 2
2011-02-23 22:31:23.058 t12[31786:903] 3
2011-02-23 22:31:23.059 t12[31786:903] 4
2011-02-23 22:31:23.059 t12[31786:903] 5
2011-02-23 22:31:23.060 t12[31786:903] 6
2011-02-23 22:31:23.060 t12[31786:903] 7
2011-02-23 22:31:23.060 t12[31786:903] 8
2011-02-23 22:31:23.061 t12[31786:903] 9
2011-02-23 22:31:23.062 t12[31786:903] 10
2011-02-23 22:31:23.062 t12[31786:903] 55

t13: scanf and increment operators

t13.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int i, userNum;

  NSLog(@"Enter a number and I will print it out: ");
  scanf("%i", &userNum);

  for(i = 1; i <= userNum; i++){
    NSLog(@"%i", i);
  }

  [pool drain];

  return 0;
}

t13.m の実行結果は:

[wtopia t13]$ ./t13
2011-02-23 22:38:51.015 t13[31841:903] Enter a number and I will print it out:
4
2011-02-23 22:38:55.206 t13[31841:903] 1
2011-02-23 22:38:55.207 t13[31841:903] 2
2011-02-23 22:38:55.208 t13[31841:903] 3
2011-02-23 22:38:55.209 t13[31841:903] 4
[wtopia t13]$ ./t13
2011-02-23 22:39:23.313 t13[31843:903] Enter a number and I will print it out:
3
2011-02-23 22:39:26.173 t13[31843:903] 1
2011-02-23 22:39:26.174 t13[31843:903] 2
2011-02-23 22:39:26.175 t13[31843:903] 3

t14: Nested for Loops

t14.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int i, j;
  int userNum;

  for(i = 1; i <= 3; i++){
    NSLog(@"Enter a number and we will print it that number of times: ");
    scanf("%i", &userNum);
    for(j = 1; j <= userNum; j++){
      NSLog(@"%i", userNum);
    }
  }

  [pool drain];
  return 0;
}

t14.m の実行結果は:

[wtopia t14]$ ./t14
2011-02-23 22:50:59.759 t14[31921:903] Enter a number and we will print it that number of times:
2
2011-02-23 22:51:06.704 t14[31921:903] 2
2011-02-23 22:51:06.710 t14[31921:903] 2
2011-02-23 22:51:06.714 t14[31921:903] Enter a number and we will print it that number of times:
3
2011-02-23 22:51:07.960 t14[31921:903] 3
2011-02-23 22:51:07.961 t14[31921:903] 3
2011-02-23 22:51:07.961 t14[31921:903] 3
2011-02-23 22:51:07.962 t14[31921:903] Enter a number and we will print it that number of times:
4
2011-02-23 22:51:08.798 t14[31921:903] 4
2011-02-23 22:51:08.799 t14[31921:903] 4
2011-02-23 22:51:08.799 t14[31921:903] 4
2011-02-23 22:51:08.799 t14[31921:903] 4

t15: while Loops

t15.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#import <Foundation/Foundation.h>

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

  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  
  int n_head = 1;
  int n_tail;
  scanf("%i", &n_tail);

  while(n_head <= 10){
    NSLog(@"%i times %i = %i", n_head, n_tail, n_head*n_tail);
    n_head++;
  }

  [pool drain];
  
  return 0;
}

t15.m の実行結果は:

[wtopia t15]$ ./t15
10
2011-02-23 22:59:26.909 t15[31981:903] 1 times 10 = 10
2011-02-23 22:59:26.964 t15[31981:903] 2 times 10 = 20
2011-02-23 22:59:26.965 t15[31981:903] 3 times 10 = 30
2011-02-23 22:59:26.966 t15[31981:903] 4 times 10 = 40
2011-02-23 22:59:26.967 t15[31981:903] 5 times 10 = 50
2011-02-23 22:59:26.968 t15[31981:903] 6 times 10 = 60
2011-02-23 22:59:26.969 t15[31981:903] 7 times 10 = 70
2011-02-23 22:59:26.970 t15[31981:903] 8 times 10 = 80
2011-02-23 22:59:26.971 t15[31981:903] 9 times 10 = 90
2011-02-23 22:59:26.971 t15[31981:903] 10 times 10 = 100

t16: do Statements

t16.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int n = 1;

  // at least one time to be runned
  do{
    NSLog(@"%i", n*n);
    n++;
  }while(n <= 5);

  [pool drain];

  return 0;
}

t16.m の実行結果は:

[wtopia t16]$ ./t16
2011-02-23 23:05:33.452 t16[32030:903] 1
2011-02-23 23:05:33.495 t16[32030:903] 4
2011-02-23 23:05:33.496 t16[32030:903] 9
2011-02-23 23:05:33.497 t16[32030:903] 16
2011-02-23 23:05:33.497 t16[32030:903] 25

t17: if else

t17.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int a = 17;
  int b = 55;

  if (a < b){
    NSLog(@"gametime");
    NSLog(@"go get her");
    NSLog(@"she is single");
  }else{
    NSLog(@"no way old man");
  }

  [pool drain];
  
  return 0;
}

t17.m の実行結果は:

[wtopia t17]$ ./t17
2011-02-23 23:13:40.808 t17[32066:903] gametime
2011-02-23 23:13:40.854 t17[32066:903] go get her
2011-02-23 23:13:40.858 t17[32066:903] she is single

t18: Useful if else Program

t18.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#import <Foundation/Foundation.h>

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

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

  int n, r;

  NSLog(@"Enter a number: ");
  scanf("%i", &n);

  r = (n % 2);

  if(r == 0){
    NSLog(@"your number is even");
  }else{
    NSLog(@"your number is odd");
  }

  [pool drain];
  return 0;
}

t18.m の実行結果は:

[wtopia t18]$ ./t18
2011-02-23 23:19:12.296 t18[32105:903] Enter a number:
1
2011-02-23 23:19:16.055 t18[32105:903] your number is odd
[wtopia t18]$ ./t18
2011-02-23 23:19:21.535 t18[32107:903] Enter a number:
0
2011-02-23 23:19:22.650 t18[32107:903] your number is even
[wtopia t18]$ ./t18
2011-02-23 23:19:26.403 t18[32108:903] Enter a number:
10
2011-02-23 23:19:28.411 t18[32108:903] your number is even

t19: Nested if Statements

t19.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int age, sex;

  NSLog(@"Enter age: ");
  scanf("%i", &age);

  NSLog(@"Enter sex(1:Boy 2:Girl): ");
  scanf("%i", &sex);

  if(age < 18 || age > 80){
    NSLog(@"Go to another website hoss");
  }else{
    if(sex == 1){
      NSLog(@"Welcome man!");
    }else{
      NSLog(@"Welcome girlie!");
    }
  }

  [pool drain];
  return 0;
}

t19.m の実行結果は:

[wtopia t19]$ ./t19
2011-02-23 23:27:05.865 t19[32156:903] Enter age:
28
2011-02-23 23:27:12.543 t19[32156:903] Enter sex(1:Boy 2:Girl):
1
2011-02-23 23:27:15.079 t19[32156:903] Welcome man!

t20: else if Statements

t20.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import <Foundation/Foundation.h>

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

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

  int time;

  NSLog(@"Enter the fn time: ");
  scanf("%i", &time);

  if(time < 11){
    NSLog(@"Good morning to ya!");
  }else if(time < 16){
    NSLog(@"Good afternoon!");
  }else if(time < 24){
    NSLog(@"Good night hoss");
  }else{
    NSLog(@"WTF did you even enter");
  }

  [pool drain];

  return 0;
}

t20.m の実行結果は:

[wtopia t20]$ ./t20
2011-02-23 23:35:14.196 t20[32194:903] Enter the fn time:
10
2011-02-23 23:35:17.150 t20[32194:903] Good morning to ya!
[wtopia t20]$ ./t20
2011-02-23 23:35:19.948 t20[32195:903] Enter the fn time:
28
2011-02-23 23:35:21.429 t20[32195:903] WTF did you even enter

t21: switch

t21.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#import <Foundation/Foundation.h>

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

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

  int age;
  scanf("%i", &age);

  switch(age){
  case 1:
    NSLog(@"they are cute");
    break;
  case 2:
    NSLog(@"they are terrible");
    break;
  case 3:
    NSLog(@"they are thirsy");
    break;
  case 4:
    NSLog(@"they are 4");
    break;
  default:
    NSLog(@"That isnt a kid, its a fn teenager");
    break;
  }

  [pool drain];

  return 0;
}

t21.m の実行結果は:

[wtopia t21]$ ./t21
1
2011-02-23 23:42:55.130 t21[32229:903] they are cute
[wtopia t21]$ ./t21
20
2011-02-23 23:43:04.474 t21[32231:903] That isnt a kid, its a fn teenager

t22: Conditional Operator

t22.m

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int a = 2;
  int b = 3;
  int c = 0;

  a == b ? NSLog(@"they are equal") : NSLog(@"different");
  c ? NSLog(@"true") : NSLog(@"false");

  if(c){
    NSLog(@"true");
  }else{
    NSLog(@"false");
  }

  [pool drain];
  return 0;
}

t22.m の実行結果は:

[wtopia t22]$ ./t22
2011-02-24 15:51:08.107 t22[32792:903] different
2011-02-24 15:51:08.163 t22[32792:903] false
2011-02-24 15:51:08.163 t22[32792:903] false

t23: Seperating Files

We use four files on this program:

1. Person.h
2. Person.m
3. t23.m (main)
4. GNUmakefile
  1. Person.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Person.h

#import <Foundation/Foundation.h>

@interface Person: NSObject{
  int age;
  int weight;
}

- (void) print;
- (void) setAge: (int) a;
- (void) setWeight: (int) w;

@end
  1. Person.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
// Person.m

#import "Person.h"

@implementation Person

- (void) print{
  NSLog(@"I am %i years old and weight %i pounds", age, weight);
}

- (void) setAge: (int) a{
  age = a;
}

- (void) setWeight: (int) w{
  weight = w;
}

@end
  1. t23.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Person *fei = [[Person alloc] init];
  [fei setAge: 28];
  [fei setWeight: 165];
  [fei print];
  [fei release];

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t23
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Person.m t23.m
OBJ = Person.o t23.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Person.o: Person.h Person.m
t23.o: t23.m

t23.m の実行結果は:

[wtopia t23]$ ./t23
2011-02-24 16:25:00.761 t23[32943:903] I am 28 years old and weight 165 pounds

t24: Running the New Program

t25: Synthesized Accessor Methods

We use four files on this program:

1. Person.h
2. Person.m
3. t25.m (main)
4. GNUmakefile
  1. Person.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Person.h

#import <Foundation/Foundation.h>

@interface Person: NSObject{
  int age;
  int weight;
}

@property int age, weight;

- (void) print;

@end
  1. Person.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Person.m

#import "Person.h"

@implementation Person

@synthesize age, weight;

- (void) print{
  NSLog(@"I am %i years old and weight %i pounds", age, weight);
}

@end
  1. t25.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Person *fei = [[Person alloc] init];
  fei.age = 28;
  fei.weight = 165;
  [fei print];
  [fei release];

  Person *julia = [[Person alloc] init];
  julia.age = 20;
  julia.weight = 100;
  [julia print];
  [julia release];

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t25
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Person.m t25.m
OBJ = Person.o t25.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Person.o: Person.h Person.m
t25.o: t25.m

t25.m の実行結果は:

[wtopia t25]$ ./t25
2011-02-24 16:48:26.399 t25[33034:903] I am 28 years old and weight 165 pounds
2011-02-24 16:48:26.519 t25[33034:903] I am 20 years old and weight 100 pounds

t26: How young of a chick can I date?

We use four files on this program:

1. Person.h
2. Person.m
3. t26.m (main)
4. GNUmakefile
  1. Person.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Person.h

#import <Foundation/Foundation.h>

@interface Person: NSObject{
  int age;
  int weight;
}

@property int age, weight;

- (void) print;
- (void) dataAge: (int) a: (int) i;

@end
  1. Person.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Person.m

#import "Person.h"

@implementation Person

@synthesize age, weight;

- (void) print{
  NSLog(@"I am %i years old and weight %i pounds", age, weight);
}

- (void) dataAge: (int) a: (int) i{
  NSLog( @"You can data chicks %i years old", (a/2+7)-(i/100000) );
}

@end
  1. t26.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Person *fei = [[Person alloc] init];

  fei.age = 28;
  fei.weight = 165;

  [fei print];
  [fei dataAge: 40: 700000];
  [fei release];

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t26
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Person.m t26.m
OBJ = Person.o t26.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Person.o: Person.h Person.m
t26.o: t26.m

t26.m の実行結果は:

[wtopia t26]$ ./t26
2011-02-24 17:01:27.591 t26[33120:903] I am 28 years old and weight 165 pounds
2011-02-24 17:01:27.707 t26[33120:903] You can data chicks 20 years old

t27: But what if I’m a millionaire?

t28: Inheritance

t28.m

#import <Foundation/Foundation.h>

@interface Bucky: NSObject{
  int a;
}

- (void) meth;

@end

@implementation Bucky

- (void) meth{
  a = 50;
}

@end

// Inheritance of Bucky
@interface Fei: Bucky{
}

- (void) printThing;

@end

@implementation Fei

- (void) printThing{
  NSLog(@"%i", a);
}

@end

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Fei *fei = [[Fei alloc] init];

  [fei meth];
  [fei printThing];
  [fei release];

  [pool drain];
  return 0;
}

t28.m の実行結果は:

[wtopia t28]$ ./t28
2011-02-24 17:15:23.286 t28[33235:903] 50

t29: My Mom

t30: Starting the Rectangle Class

We use four files on this program:

1. Rectangle.h
2. Rectangle.m
3. t30.m (main)
4. GNUmakefile
  1. Rectangle.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Rectangle.h
#import <Foundation/Foundation.h>

@interface Rectangle: NSObject{
  int width;
  int height;
}

@property int width, height;

- (int) area;
- (int) perimeter;
- (void) setWH: (int)w: (int)h;

@end
  1. Rectangle.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Rectangle.m

#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height; // setters and getters

- (int) area{
  return width*height;
}

- (int) perimeter{
  return (width+height)*2;
}

- (void) setWH: (int) w: (int) h{
  width = w;
  height = h;
}

@end
  1. t30.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#import <Foundation/Foundation.h>
#import "Rectangle.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Rectangle *r = [[Rectangle alloc] init];
  [r setWH: 11: 11];

  NSLog(@"Rectangle is %i by %i", r.width, r.height);
  NSLog(@"Area = %i, Perimeter = %i", [r area], [r perimeter]);

  [r release];

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t30
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Rectangle.m t30.m
OBJ = Rectangle.o t30.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Rectangle.o: Rectangle.h Rectangle.m
t30.o: t30.m

t30.m の実行結果は:

[wtopia t30]$ ./t30
2011-02-24 17:46:40.961 t30[33348:903] Rectangle is 11 by 11
2011-02-24 17:46:41.014 t30[33348:903] Area = 121, Perimeter = 44

t31: Finishing the Rectangle Class

t32: OMG, a Square Class?

We use six files on this program:

1. Rectangle.h
2. Rectangle.m
3. Square.h
4. Square.m
5. t32.m (main)
6. GNUmakefile
  1. Rectangle.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Rectangle.h
#import <Foundation/Foundation.h>

@interface Rectangle: NSObject{
  int width;
  int height;
}

@property int width, height;

- (int) area;
- (int) perimeter;
- (void) setWH: (int) w: (int) h;

@end
  1. Rectangle.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Rectangle.m

#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;

- (int) area{
  return width*height;
}

- (int) perimeter{
  return (width+height)*2;
}

- (void) setWH: (int) w: (int) h{
  width = w;
  height = h;
}

@end
  1. Square.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Square.h

#import "Rectangle.h"

@interface Square: Rectangle

- (void) setSide: (int) s;
- (int) side;

@end
  1. Square.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Square.m

#import "Square.h"

@implementation Square: Rectangle

- (void) setSide: (int) s{
  [self setWH: s: s];
}

- (int) side{
  return width;
}

@end
  1. t32.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#import <Foundation/Foundation.h>
#import "Square.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Square *square = [[Square alloc] init];
  [square setSide: 10];
  NSLog(@"One side is %d", [square side]);

  // methods from inherited Rectangle class
  NSLog(@"Area is %i, Perimeter is %i", [square area], [square perimeter]);

  [square release];

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t32
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Square.m Rectangle.m t32.m
OBJ = Square.o Rectangle.o t32.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Square.o: Square.h Square.m
Rectangle.o: Rectangle.h Rectangle.m
t32.o: t32.m

t32.m の実行結果は:

[wtopia t32]$ ./t32
2011-02-24 18:08:32.905 t32[33551:903] One side is 10
2011-02-24 18:08:32.987 t32[33551:903] Area is 100, Perimeter is 40

t33: Making steve the Square

t34: Creating a Point Class

We use eight files on this program:

1. Rectangle.h
2. Rectangle.m
3. Square.h
4. Square.m
5. XYPoint.h
6. XYPoint.m
7. t34.m (main)
8. GNUmakefile
  1. Rectangle.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// Rectangle.h

#import <Foundation/Foundation.h>

@class XYPoint;

@interface Rectangle: NSObject{
  int width;
  int height;
  XYPoint *orgin;
}

@property int width, height;

- (XYPoint *) orgin;
- (void) setOrgin: (XYPoint *) pt;
- (int) area;
- (int) perimeter;
- (void) setWH: (int) w: (int) h;

@end
  1. Rectangle.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Rectangle.m

#import "Rectangle.h"

@implementation Rectangle

@synthesize width, height;

- (int) area{
  return width*height;
}

- (int) perimeter{
  return (width+height)*2;
}

- (void) setWH: (int) w: (int) h{
  width = w;
  height = h;
}

- (XYPoint *) orgin{
  return orgin;
}

- (void) setOrgin: (XYPoint *) pt{
  orgin = pt;
}

@end
  1. Square.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Square.h

#import "Rectangle.h"

@interface Square: Rectangle

- (void) setSide: (int) s;
- (int) side;

@end
  1. Square.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Square.m

#import "Square.h"

@implementation Square: Rectangle

- (void) setSide: (int) s{
  [self setWH: s: s];
}

- (int) side{
  return width;
}

@end
  1. XYPoint.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// XYPoint.h

#import <Foundation/Foundation.h>

@interface XYPoint: NSObject{
  int x;
  int y;
}

@property int x, y;

- (void) setXY: (int) xVal: (int) yVal;

@end
  1. XYPoint.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// XYPoint.m

#import "XYPoint.h"

@implementation XYPoint

@synthesize x, y;

- (void) setXY: (int) xVal: (int) yVal{
  x = xVal;
  y = yVal;
}

@end
  1. t34.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import <Foundation/Foundation.h>
#import "Rectangle.h"
#import "XYPoint.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Rectangle *r = [[Rectangle alloc] init];
  XYPoint *p = [[XYPoint alloc] init];

  [p setXY: 100: 300];
  [r setWH: 6: 8];

  r.orgin = p;
  NSLog(@"Width and Heith is %i %i", r.width, r.height);
  NSLog(@"Orgin at %i %i", r.orgin.x, r.orgin.y);
  NSLog(@"Area and perimeter are %i and %i", [r area], [r perimeter]);

  [r release];
  [p release];

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t34
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Square.m Rectangle.m XYPoint.m t34.m
OBJ = Square.o Rectangle.o XYPoint.o t34.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Square.o: Square.h Square.m
XYPoint.o: XYPoint.h XYPoint.m
Rectangle.o: Rectangle.h Rectangle.m
t34.o: t34.m

t34.m の実行結果は:

[wtopia t34]$ ./t34
2011-02-24 19:22:08.351 t34[33863:903] Width and Heith is 6 8
2011-02-24 19:22:08.354 t34[33863:903] Orgin at 100 300
2011-02-24 19:22:08.354 t34[33863:903] Area and perimeter are 48 and 28

t35: Enhancing the Rectangle Class

t36: Running the Brand New Point Program

t37: Overringding Methods

We use six files on this program:

1. Son.h
2. Son.m
3. Mom.h
4. Mom.m
5. t37.m
6. GNUmakefile
  1. Son.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Son.h

#import "Mom.h"

@interface Son: Mom{
  int newVar;
}

- (void) setNum1;

- (void) printNumber;

@end
  1. Son.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// Son.m

#import "Son.h"

@implementation Son

- (void) setNum1{
  num1 = 14;
  newVar = 69;
}

- (void) printNumber{
  NSLog(@"The number is %i", num1);
  NSLog(@"The number is %i", newVar);
}

@end
  1. Mom.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Mom.h

#import <Foundation/Foundation.h>

@interface Mom: NSObject{
  int num1;
}

- (void) setNum1;

@end
  1. Mom.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// Mom.m

#import "Mom.h"

@implementation Mom

- (void) setNum1{
  num1 = 70;
}

@end
  1. t37.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#import <Foundation/Foundation.h>
#import "Son.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Son *s = [[Son alloc] init];

  [s setNum1];
  [s printNumber];
  [s release];

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t37
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Son.m Mom.m t37.m
OBJ = Son.o Mom.o t37.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Son.o: Son.h Son.m
Mom.o: Mom.h Mom.m
t37.o: t37.m

t37.m の実行結果は:

[wtopia t37]$ ./t37
2011-02-24 21:02:48.216 t37[34091:903] The number is 14
2011-02-24 21:02:48.218 t37[34091:903] The number is 69

t38: New Instance Variables and Abstract Classes

t39: Introduction to Polymorphism

Polymorphism is the ability to have methods with the same name in different classes.

We use six files on this program:

1. Charz.h
2. Charz.m
3. Numz.h
4. Numz.m
5. t39.m
6. GNUmakefile
  1. Charz.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// Charz.h

#import <Foundation/Foundation.h>

@interface Charz: NSObject{
  char c1;
  char c2;
}

- (void) setCharz;
- (void) add;
- (void) print;

@end
  1. Charz.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Charz.m

#import "Charz.h"

@implementation Charz

- (void) setCharz{
  c1 = 'B';
  c2 = 'W';
}

- (void) add{
  NSLog(@"%c%c", c1, c2);
}

- (void) print{
  NSLog(@"I am from the Charz class more");
}

@end
  1. Numz.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Numz.h

#import <Foundation/Foundation.h>

@interface Numz: NSObject{
  int num1;
  int num2;
  int answer;
}

- (void) setNumbers: (int) a: (int) b;
- (void) add;
- (void) print;

@end
  1. Numz.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Numz.m

#import "Numz.h"

@implementation Numz

- (void) setNumbers: (int) a: (int) b{
  num1 = a;
  num2 = b;
}

- (void) add{
  answer = num1 + num2;
}

- (void) print{
  NSLog(@"I am the Numz class, %i", answer);
}

@end
  1. t39.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#import <Foundation/Foundation.h>
#import "Charz.h"
#import "Numz.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  id fei; // Dynamic Binding and id
  Numz *n = [[Numz alloc] init];
  Charz *c = [[Charz alloc] init];

  fei = n;
  [fei setNumbers: 8: 10];
  [fei add];
  [fei print];

  fei = c;
  [fei setCharz];
  [fei add];
  [fei print];

  // Polymorphism' (same method different class)
  [n setNumbers: 8: 10];
  [n add];
  [n print];

  [c setCharz];
  [c add];
  [c print];
  // Polymorphism'' (same method different class)

  [n release];
  [c release];

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t39
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Charz.m Numz.m t39.m
OBJ = Charz.o Numz.o t39.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Charz.o: Charz.h Charz.m
Numz.o: Numz.h Numz.m
t39.o: t39.m

t39.m の実行結果は:

[wtopia t39]$ ./t39
2011-02-24 21:34:45.518 t39[34301:903] I am the Numz class, 18
2011-02-24 21:34:45.522 t39[34301:903] BW
2011-02-24 21:34:45.528 t39[34301:903] I am from the Charz class more
2011-02-24 21:34:45.528 t39[34301:903] I am the Numz class, 18
2011-02-24 21:34:45.529 t39[34301:903] BW
2011-02-24 21:34:45.529 t39[34301:903] I am from the Charz class more

t40: Running a Polymorphic Program

t41: Dynamic Binding and id

t42: Error / Exception Handling

We use four files on this program:

1. Numz.h
2. Numz.m
3. t42.m
4. GNUmakefile
  1. Numz.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Numz.h

#import <Foundation/Foundation.h>

@interface Numz: NSObject{
  int num1;
  int num2;
  int answer;
}

- (void) setNumbers: (int) a: (int) b;
- (void) add;
- (void) print;

@end
  1. Numz.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Numz.m

#import "Numz.h"

@implementation Numz

- (void) setNumbers: (int) a: (int) b{
  num1 = a;
  num2 = b;
}

- (void) add{
  answer = num1 + num2;
}

- (void) print{
  NSLog(@"I am from the Numz class, %i", answer);
}

@end
  1. t42.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#import <Foundation/Foundation.h>
#import "Numz.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Numz *n = [[Numz alloc] init];

  @try{
    [n testthismethod]; // using undeclared method
  }
  @catch(NSException *e){
    [n setNumbers: 10: 18];
    [n add];
    [n print];
  }
  @finally{
    [n setNumbers: 100: 180];
    [n add];
    [n print];
  }

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t42
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Numz.m t42.m
OBJ = Numz.o t42.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Numz.o: Numz.h Numz.m
t42.o: t42.m

t42.m の実行結果は:

[wtopia t42]$ make
gcc -c -g -Wall Numz.m
gcc -c -g -Wall t42.m
t42.m: In function ‘main’:
t42.m:10: warning: ‘Numz’ may not respond to ‘-testthismethod’
t42.m:10: warning: (Messages without a matching method signature
t42.m:10: warning: will be assumed to return ‘id’ and accept
t42.m:10: warning: ‘...’ as arguments.)
gcc -g -Wall -o t42 Numz.o t42.o -framework Foundation
[wtopia t42]$ ./t42
2011-02-24 22:06:09.133 t42[34700:903] -[Numz testthismethod]: unrecognized selector sent to instance 0x100108d20
2011-02-24 22:06:09.189 t42[34700:903] I am from the Numz class, 28
2011-02-24 22:06:09.192 t42[34700:903] I am from the Numz class, 280

t43: Directives for Controlling Scope

We use four files on this program:

1. Tuna.h
2. Tuna.m
3. t43.m (main)
4. GNUmakefile
  1. Tuna.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// Tuna.h

#import <Foundation/Foundation.h>

@interface Tuna: NSObject{

@private
  int y;
  int g;

@protected
  int h;
  int e;
}

- (void) changeVar;

@end
  1. Tuna.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Tuna.m

#import "Tuna.h"

@implementation Tuna

- (void) changeVar{
  extern int gDrunk;
  gDrunk = 100;
}

@end
  1. t43.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#import <Foundation/Foundation.h>
#import "Tuna.h"

int gDrunk = 21;

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSLog(@"%i", gDrunk);

  Tuna *fish = [[Tuna alloc] init];
  [fish changeVar];
  NSLog(@"%i", gDrunk);

  [fish release];

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t43
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Tuna.m t43.m
OBJ = Tuna.o t43.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Tuna.o: Tuna.h Tuna.m
t43.o: t43.m

t43.m の実行結果は:

[wtopia t43]$ ./t43
2011-02-25 17:38:51.260 t43[27191:903] 21
2011-02-25 17:38:51.263 t43[27191:903] 100

t44: External / Global Variables

t45: Static Variables

We use four files on this program:

1. Tuna.h
2. Tuna.m
3. t45.m (main)
4. GNUmakefile
  1. Tuna.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Tuna.h

#import <Foundation/Foundation.h>

@interface Tuna: NSObject

- (void) addOne;
- (void) printIt;

@end
  1. Tuna.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Tuna.m
#import "Tuna.h"

static int gx = 0; // only makes it global  not external

@implementation Tuna

- (void) addOne{
  gx++;
}

- (void) printIt{
  NSLog(@"The number is %i", gx);
}

@end
  1. t45.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#import <Foundation/Foundation.h>
#import "Tuna.h"

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Tuna *fish = [[Tuna alloc] init];
  
  [fish addOne]; // static gx = 0 + 1 = 1
  [fish printIt];

  [fish addOne]; // static gx = 1 + 1 = 2
  [fish printIt];

  [fish addOne]; // static gx = 2 + 1 = 3
  [fish printIt];

  [fish addOne]; // static gx = 3 + 1 = 4
  [fish printIt];

  [fish addOne]; // static gx = 4 + 1 = 5
  [fish printIt];

  [fish release];

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t45
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = Tuna.m t45.m
OBJ = Tuna.o t45.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

Tuna.o: Tuna.h Tuna.m
t45.o: t45.m

t45.m の実行結果は:

[wtopia t45]$ ./t45
2011-02-25 17:52:04.277 t45[27339:903] The number is 1
2011-02-25 17:52:04.280 t45[27339:903] The number is 2
2011-02-25 17:52:04.281 t45[27339:903] The number is 3
2011-02-25 17:52:04.281 t45[27339:903] The number is 4
2011-02-25 17:52:04.282 t45[27339:903] The number is 5

t46: Enumerated Data Types

We use two files on this program:

1. t46.m (main)
2. GNUmakefile
  1. t46.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int dayValue;

  enum day{
    m = 1,
    t,
    w,
    h,
    f
  }; // default value is from 0 ~
  enum day entry;

  NSLog(@"Enter a number of day of the week");
  scanf("%i", &dayValue);
  entry = (enum day) dayValue;

  switch(entry){
  case m:
    NSLog(@"that day is monday");
    break;
  case t:
    NSLog(@"that day is tuesday");
    break;
  case w:
    NSLog(@"that day is wednesday");
    break;
  case h:
    NSLog(@"that day is thursday");
    break;
  case f:
    NSLog(@"that day is friday");
    break;
  default:
    break;
  }

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t46
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t46.m
OBJ = t46.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t46.o: t46.m

t46.m の実行結果は:

[wtopia t46]$ ./t46
2011-02-25 18:23:26.460 t46[27631:903] Enter a number of day of the week
1
2011-02-25 18:23:29.284 t46[27631:903] that day is monday
[wtopia t46]$ ./t46
2011-02-25 18:23:39.164 t46[27632:903] Enter a number of day of the week
5
2011-02-25 18:23:41.372 t46[27632:903] that day is friday
[wtopia t46]$ ./t46
2011-02-25 18:23:43.989 t46[27633:903] Enter a number of day of the week
7

t47: Enum Program

t48: Categories

We use four files on this program:

1. Numz.h
2. Numz.m
3. t48.m (main)
4. GNUmakefile
  1. Numz.h
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
// Numz.h

#import <Foundation/Foundation.h>

@interface Numz: NSObject{
  int num1;
  int num2;
  int answer;
}

- (void) setNumbers: (int) a: (int) b;
- (void) add;
- (void) print;

@end
  1. Numz.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
// Numz.m

#import "Numz.h"

@implementation Numz

- (void) setNumbers: (int) a: (int) b{
  num1 = a;
  num2 = b;
}

- (void) add{
  answer = num1 + num2;
}

- (void) print{
  NSLog(@"I am from the Numz class, %i", answer);
}

@end
  1. t48.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#import <Foundation/Foundation.h>
#import "Numz.h"

// Categories
@interface Numz (moreMethod)
- (void) sub: (int) a: (int) b;
- (void) mul: (int) a: (int) b;
@end

@implementation Numz (moreMethod)

- (void) sub: (int) a: (int) b{
  NSLog(@"These numbers subtracted are %i", a - b);
}

- (void) mul: (int) a: (int) b{
  NSLog(@"These numbers multiplied are %i", a * b);
}

@end

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  Numz *n = [[Numz alloc] init];

  [n sub: 100: 50];
  [n mul: 100: 40];
  [n release];

  [pool drain];
  
  return 0;
}

4. GNUmakefile .. literalinclude:: oc.all/t48/GNUmakefile

Language:objective-c
linenos:

t48.m の実行結果は:

[wtopia t48]$ ./t48
2011-02-25 18:41:43.065 t48[27738:903] These numbers subtracted are 50
2011-02-25 18:41:43.108 t48[27738:903] These numbers multiplied are 4000

t49: #define Statement

We use two files on this program:

1. t49.m (main)
2. GNUmakefile
  1. t49.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#import <Foundation/Foundation.h>

#define PI 3.14159
#define TWOPI 2.0*PI

#define AND &&
#define OR ||

#define EQUALS ==

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  int a = 10;
  int b = 20;
  int c = 20;

  float x = PI + 6;
  NSLog(@"%f", x);

  if(a EQUALS b)
    NSLog(@"a is equal to b");
  NSLog(@"a is not equal to b");

  if(a > b AND b > c)
    NSLog(@"a is greater than c");
  NSLog(@"a is less than c");

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t49
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t49.m
OBJ = t49.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t49.o: t49.m

t49.m の実行結果は:

[wtopia t49]$ ./t49
2011-02-25 18:56:57.771 t49[27823:903] 9.141590
2011-02-25 18:56:57.774 t49[27823:903] a is not equal to b
2011-02-25 18:56:57.775 t49[27823:903] a is less than c

t50: #import Statement

We use three files on this program:

1. crap.h
2. t50.m (main)
3. GNUmakefile
  1. crap.h
1
2
3
4
// crap.h

#define BACON 23
#define TOAST 44
  1. t50.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#import <Foundation/Foundation.h>
#import "crap.h"

#define EVEN(x) x % 2 == 0

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  if( EVEN(20) )
    NSLog(@"it s even");
  NSLog(@"it is odd");

  int price = BACON + TOAST;
  NSLog(@"the price of BACON and TOAST is %i", price);

  [pool drain];

  return 0;  
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t50
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t50.m
OBJ = t50.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

crap.o: crap.h
t50.o: t50.m

t50.m の実行結果は:

[wtopia t50]$ ./t50
2011-02-25 21:19:40.972 t50[42279:903] it s even
2011-02-25 21:19:41.012 t50[42279:903] it is odd
2011-02-25 21:19:41.012 t50[42279:903] the price of BACON and TOAST is 67

t51: Introduction to Number Objects

We use two files on this program:

1. t51.m (main)
2. GNUmakefile
  1. t51.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSNumber *n1;
  NSNumber *n2;

  n1 = [NSNumber numberWithInt:123];
  n2 = [NSNumber numberWithInt:456];

  int x = [n1 intValue];
  int y = [n2 intValue];

  NSLog(@"%i", x);
  NSLog(@"%i", y);

  if( [n1 isEqualToNumber:n2] ){
    NSLog(@"they are equal");
  }else{
    NSLog(@"they are not equal");
  }

  NSNumber *num1 = [NSNumber numberWithInt: 3];
  NSNumber *num2 = [NSNumber numberWithInt: 2];

  switch( [num1 compare:num2] ){
  case NSOrderedAscending:
    NSLog(@"NSOrderedAscending");
    break;
  case NSOrderedSame:
    NSLog(@"NSOrderedSame");
    break;
  case NSOrderedDescending:
    NSLog(@"NSOrderedDescending");
    break;
  default:
    NSLog(@"default");
    break;
  }
  
  // if we use release method it'll get segmentation falult error
  // [n1 release];
  // [n2 release];
  // [num1 release];
  // [num2 release];

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t51
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t51.m
OBJ = t51.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t51.o: t51.m

t51.m の実行結果は:

[wtopia t51]$ make
gcc -c -g -Wall t51.m
gcc -g -Wall -o t51 t51.o -framework Foundation
[wtopia t51]$ ./t51
2011-02-25 22:00:32.105 t51[42830:903] 123
2011-02-25 22:00:32.108 t51[42830:903] 456
2011-02-25 22:00:32.108 t51[42830:903] they are not equal
2011-02-25 22:00:32.109 t51[42830:903] NSOrderedAscending
[wtopia t51]$ make
gcc -c -g -Wall t51.m
gcc -g -Wall -o t51 t51.o -framework Foundation
[wtopia t51]$ ./t51
2011-02-25 22:00:52.031 t51[42848:903] 123
2011-02-25 22:00:52.035 t51[42848:903] 456
2011-02-25 22:00:52.036 t51[42848:903] they are not equal
2011-02-25 22:00:52.037 t51[42848:903] NSOrderedDescending
[wtopia t51]$ make
gcc -c -g -Wall t51.m
gcc -g -Wall -o t51 t51.o -framework Foundation
[wtopia t51]$ ./t51
2011-02-25 22:01:04.695 t51[42865:903] 123
2011-02-25 22:01:04.699 t51[42865:903] 456
2011-02-25 22:01:04.700 t51[42865:903] they are not equal
2011-02-25 22:01:04.700 t51[42865:903] NSOrderedSame

Ascending 昇順

Descending 降順

t52: Number Object Methods

t53: String Objects

We use two files on this program:

1. t53.m (main)
2. GNUmakefile
  1. t53.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSString *str = @"i don't like yakiniku";
  NSString *tester;
  NSString *upper;

  upper = [str uppercaseString];
  NSLog(@"Upper case is %@", upper);

  tester = [NSString stringWithString:str];
  NSLog(@"copy NSString str is \" %@ \""), tester;

  NSLog(@"%@", str);
  NSLog(@"Length of NSString str is %i", [str length]);

  NSNumber *n = [NSNumber numberWithInt:120];
  NSLog(@"%@", n);

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t53
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t53.m
OBJ = t53.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t53.o: t53.m

t53.m の実行結果は:

[wtopia t53]$ ./t53
2011-02-25 22:38:30.884 t53[43124:903] Upper case is I DON'T LIKE YAKINIKU
2011-02-25 22:38:30.887 t53[43124:903] copy NSString str is " i don't like yakiniku "
2011-02-25 22:38:30.887 t53[43124:903] i don't like yakiniku
2011-02-25 22:38:30.888 t53[43124:903] Length of NSString str is 21
2011-02-25 22:38:30.888 t53[43124:903] 120

t54: Substrings and Ranges

NSRange Definition

NSRange is a structure defined as follows:

typedef struct _NSRange{
  NSUInteger location;
  NSUInteger length;
}NSRange;

NSMakeRange:

(NSRange) NSMakeRange(unsigned int location, unsigned int length)

返り値: NSRange
引数: location, length

We use two files on this program:

1. t54.m (main)
2. GNUmakefile
  1. t54.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSString *s = @"I don't like yakiniku";
  NSString *d = @"Don't feed grapes to dogs";
  NSString *tester;

  NSRange range = [d rangeOfString:@"dogs"];
  NSLog(@"Location is %i and length is %i", range.location, range.length);

  tester = [d substringToIndex:10];
  NSLog(@"First tens chars are: %@", tester);

  tester = [s substringFromIndex:2];
  NSLog(@"From Index 2 til end is: %@", tester);

  tester = [d substringWithRange:NSMakeRange(6, 4)];
  NSLog(@"From Index 6 til end Index 14 is: %@", tester);

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t54
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t54.m
OBJ = t54.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t53.o: t53.m

t54.m の実行結果は:

[wtopia t54]$ ./t54
2011-02-25 23:19:40.122 t54[43366:903] Location is 21 and length is 4
2011-02-25 23:19:40.127 t54[43366:903] First tens chars are: Don't feed
2011-02-25 23:19:40.128 t54[43366:903] From Index 2 til end is: don't like yakiniku
2011-02-25 23:19:40.128 t54[43366:903] From Index 6 til end Index 14 is: feed

t55: Mutable Strings

NSMutableString:

- (void) insertString: (NSString *) astring
         atIndex: (unsigned) anIndex
返り値: void (なし)
パラメータ: aString (挿入する文字)
            anIndex (挿入する位置)

We use two files on this program:

1. t55.m (main)
2. GNUmakefile
  1. t55.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  
  NSString *dog = @"Hotdog? I thought you said pumpkin!";
  NSMutableString *mute;

  mute = [NSMutableString stringWithString:dog];
  NSLog(@"%@", mute);

  [mute insertString:@" sauce " atIndex:3];
  NSLog(@"%@", mute);

  [mute appendString:@" My bad!"];
  NSLog(@"%@", mute);

  // delete Hot from mute
  [mute deleteCharactersInRange:NSMakeRange(0, 3)];
  NSLog(@"%@", mute);

  [pool drain];
  return 0;
  
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t55
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t55.m
OBJ = t55.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t55.o: t55.m

t55.m の実行結果は:

[wtopia t55]$ ./t55
2011-02-25 23:36:28.599 t55[43539:903] Hotdog? I thought you said pumpkin!
2011-02-25 23:36:28.638 t55[43539:903] Hot sauce dog? I thought you said pumpkin!
2011-02-25 23:36:28.639 t55[43539:903] Hot sauce dog? I thought you said pumpkin! My bad!
2011-02-25 23:36:28.640 t55[43539:903]  sauce dog? I thought you said pumpkin! My bad!

t56: Finishing Mutable Strings

We use two files on this program:

1. t56.m (main)
2. GNUmakefile
  1. t56.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSString *dog = @"Hotdog? I thought you said pumpkin!";
  NSMutableString *mute;

  mute = [NSMutableString stringWithString:dog];
  NSLog(@"%@", mute);

  [mute setString:@"I am a new string mofo!"];
  NSLog(@"%@", mute);

  [mute replaceCharactersInRange:NSMakeRange(11, 12) withString:@"mother"];
  NSLog(@"%@", mute);

  NSString *old = @"mother";
  NSString *new = @"baby seal";

  NSRange therange = [mute rangeOfString:old];
  [mute replaceCharactersInRange:therange withString:new];
  NSLog(@"%@", mute);
  
  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t56
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t56.m
OBJ = t56.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t56.o: t56.m

t56.m の実行結果は:

[wtopia t56]$ ./t56
2011-02-25 23:55:49.311 t56[43677:903] Hotdog? I thought you said pumpkin!
2011-02-25 23:55:49.314 t56[43677:903] I am a new string mofo!
2011-02-25 23:55:49.314 t56[43677:903] I am a new mother
2011-02-25 23:55:49.319 t56[43677:903] I am a new baby seal

t57: Introduction to Arrays

We use two files on this program:

1. t57.m (main)
2. GNUmakefile
  1. t57.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSArray *food = [NSArray arrayWithObjects:@"apples", @"bacon", @"corn", @"donuts", @"elfs", @"fidge", nil];
  NSLog(@"%@", [food objectAtIndex:2]);
  NSLog(@"\n");

  int i;
  for(i = 0; i < 6; i++){
    NSLog(@"items at index %i is %@", i, [food objectAtIndex:i]);
  }

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t57
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t57.m
OBJ = t57.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t57.o: t57.m

t57.m の実行結果は:

[wtopia t57]$ ./t57
2011-02-26 00:18:43.868 t57[43741:903] corn
2011-02-26 00:18:43.871 t57[43741:903]
2011-02-26 00:18:43.872 t57[43741:903] items at index 0 is apples
2011-02-26 00:18:43.872 t57[43741:903] items at index 1 is bacon
2011-02-26 00:18:43.873 t57[43741:903] items at index 2 is corn
2011-02-26 00:18:43.873 t57[43741:903] items at index 3 is donuts
2011-02-26 00:18:43.874 t57[43741:903] items at index 4 is elfs
2011-02-26 00:18:43.874 t57[43741:903] items at index 5 is fidge

t58: Mutable Arrays

NSMutableArray (instance method):

- (void) addObject: (id) anObject
返り値: void (なし)
パラメータ: anObject (追加するオブジェクト)

We use two files on this program:

1. t58.m (main)
2. GNUmakefile
  1. t58.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSMutableArray *changeme = [NSMutableArray arrayWithCapacity:2];
  
  [changeme addObject:[NSNumber numberWithInteger:2]];
  [changeme addObject:[NSNumber numberWithInteger:4]];
  
  int i, j;
  
  for(i = 6; i <= 10; i+=2){
    [changeme addObject:[NSNumber numberWithInteger:i]];
  }

  for(j = 0; j < [changeme count]; j++){
    NSLog(@"items here is : %i", [[changeme objectAtIndex:j] integerValue]);
  }

  [pool drain];

  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t58
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t58.m
OBJ = t58.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t58.o: t58.m

t58.m の実行結果は:

[wtopia t58]$ ./t58
2011-02-26 00:30:06.185 t58[43819:903] items here is : 2
2011-02-26 00:30:06.187 t58[43819:903] items here is : 4
2011-02-26 00:30:06.188 t58[43819:903] items here is : 6
2011-02-26 00:30:06.188 t58[43819:903] items here is : 8
2011-02-26 00:30:06.189 t58[43819:903] items here is : 10

t59: Dictionary Objects

We use two files on this program:

1. t59.m (main)
2. GNUmakefile
  1. t59.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  // Dictionary Objects
  NSMutableDictionary *mydic = [NSMutableDictionary dictionary];

  [mydic setObject:@"when u spary yourself with frebreeze" forKey:@"free shower"];
  [mydic setObject:@"gamer who lacks experience" forKey:@"n00b"];
  [mydic setObject:@"worst food one he pleanter" forKey:@"sushi"];

  NSLog(@"%@", [mydic objectForKey:@"sushi"]);
  NSLog(@"%@", [mydic objectForKey:@"n00b"]);
  NSLog(@"%@", [mydic objectForKey:@"free shower"]);

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t59
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t59.m
OBJ = t59.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t59.o: t59.m

t59.m の実行結果は:

[wtopia t59]$ ./t59
2011-02-26 00:40:08.376 t59[43864:903] worst food one he pleanter
2011-02-26 00:40:08.380 t59[43864:903] gamer who lacks experience
2011-02-26 00:40:08.380 t59[43864:903] when u spary yourself with frebreeze

t60: Working with Files

we should make 1.txt and del.txt two files for this program:

1.txt will be coyied to 2.txt, 2.txt will be renamed to 3.txt.
del.txt will be deleted by this program.
the size of file 3.txt will be outputed.

We use two files on this program:

1. t60.m (main)
2. GNUmakefile
  1. t60.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSString *testerfile = @"1.txt"; // file path

  NSFileManager *manager;
  manager = [NSFileManager defaultManager];

  if( [manager fileExistsAtPath:testerfile] == NO ){
    NSLog(@"File does NOT exist!");
    return 1;
  }

  // make a copy of the file
  if ( [manager copyItemAtPath:testerfile toPath:@"2.txt" error:NULL] == NO ){
    NSLog(@"can not COPY the file");
    return 2;
  }

  // rename the copy
  if( [manager moveItemAtPath:@"2.txt" toPath:@"3.txt" error:NULL] == NO){
    NSLog(@"can not rename the file");
    return 3;
  }

  // get size of the file
  NSDictionary *mydic;
  if ( (mydic = [manager attributesOfItemAtPath:@"3.txt" error:NULL]) == nil ){
    NSLog(@"could not get file attributes");
    return 4;
  }else{
    NSLog( @"File is %i bytes", [ [mydic objectForKey:NSFileSize] intValue ] );
  }

  // delete a file
  [manager removeItemAtPath:@"del.txt" error:NULL];

  // print a file
  NSLog(@"%@", [NSString stringWithContentsOfFile:testerfile encoding:NSUTF8StringEncoding error:NULL]);

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t60
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t60.m
OBJ = t60.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t60.o: t60.m

#Student.o: Person.h Student.h Student.m
#Teacher.o: Person.h Teacher.h Teacher.m
#main.o: Student.h Teacher.h

t60.m の実行結果は:

before running this program:
[wtopia t60]$ ls
1.txt         del.txt         t60.m
GNUmakefile   t60*            t60.o

after running this program:
[wtopia t60]$ ./t60
2011-02-27 00:49:11.009 t60[2084:903] File is 0 bytes
[wtopia t60]$ ls
1.txt         GNUmakefile     t60.m
3.txt         t60*            t60.o

t61: Copy and Rename Files

t62: Working with File Attributes

t63: Deleting and Printing Files

t64: Working with Directories

We use two files on this program:

1. t64.m (main)
2. GNUmakefile
  1. t64.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSFileManager *manager = [NSFileManager defaultManager];

  NSString *path;

  // get current directory
  path = [manager currentDirectoryPath];
  NSLog(@"%@", path);

  // create a new directory
  if ([manager createDirectoryAtPath:@"newdir" withIntermediateDirectories:NO attributes:nil error:NULL] == NO ){
    NSLog(@"could not create a new directory");
    return 1;
  }

  // rename that directory
  [manager moveItemAtPath:@"newdir" toPath:@"feifei" error:NULL];

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t64
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t64.m
OBJ = t64.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t64.o: t64.m

#Student.o: Person.h Student.h Student.m
#Teacher.o: Person.h Teacher.h Teacher.m
#main.o: Student.h Teacher.h

t64.m の実行結果は:

before running this program:
[wtopia t64]$ ls
GNUmakefile   t64*            t64.m           t64.o

after running this program:
[wtopia t64]$ ./t64
2011-02-27 00:59:14.987 t64[2161:903] /Users/wtopia/src_python/sphinx/oc.all/t64
[wtopia t64]$ ls
GNUmakefile   t64*            t64.o
feifei/               t64.m

t65: Read and Write Files

We use two files on this program:

1. t65.m (main)
2. GNUmakefile
  1. t65.m (main)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#import <Foundation/Foundation.h>

int main(int argc, const char *argv[]){
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSFileHandle *fin, *fout;
  NSData *buffer;

  // open fei.txt for reading
  fin = [NSFileHandle fileHandleForReadingAtPath:@"fei.txt"];

  [[NSFileManager defaultManager] createFileAtPath:@"julia.txt" contents:nil attributes:nil];

  fout = [NSFileHandle fileHandleForWritingAtPath:@"julia.txt"];

  // truncate file
  [fout truncateFileAtOffset:0];
  buffer = [fin readDataToEndOfFile];

  [fout writeData:buffer];

  [fin closeFile];
  [fout closeFile];

  [pool drain];
  return 0;
}
  1. GNUmakefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
.SUFFIXES: .o .m
.m.o:
	$(CC) -c $(CFLAGS) $<

PROG = t65
CC = gcc
CFLAGS = -g -Wall
FRAMEWORKS = -framework Foundation
SRC = t65.m
OBJ = t65.o

hist: $(OBJ)
	$(CC) $(CFLAGS) -o $(PROG) $(OBJ) $(FRAMEWORKS)

.PHONY: clean
clean:
	$(RM) $(PROG) $(OBJ) *~ a.out

t65.o: t65.m

t65.m の実行結果は:

before running this program:
[wtopia t65]$ ls
GNUmakefile   t65*            t65.o
fei.txt               t65.m

after runnning this program:
[wtopia t65]$ ./t65
[wtopia t65]$ ls
GNUmakefile   julia.txt       t65.m
fei.txt               t65*            t65.o
[wtopia t65]$ cat julia.txt
I am feifei, from China.

Table Of Contents

Previous topic

Objective-C

Next topic

Objective-C Developer Reference by Jiva DeVoe