Skip to content

Instantly share code, notes, and snippets.

@klen
Created November 9, 2013 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klen/7383993 to your computer and use it in GitHub Desktop.
Save klen/7383993 to your computer and use it in GitHub Desktop.
Objective C - tutorial
//
// City.h
// city
//
// Created by Kirill Klenov on 09.11.13.
// Copyright (c) 2013 Kirill Klenov. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface City : NSObject {
NSString* name;
int age;
int population;
}
- (void) setName: (NSString*) n;
- (NSString*) getName;
- (void) setAge: (int) a;
- (int) getAge;
- (void) setPopulation: (int) p;
- (int) getPopulation;
- (void) nextDay;
- (void) print;
@end
//
// City.m
// city
//
// Created by Kirill Klenov on 09.11.13.
// Copyright (c) 2013 Kirill Klenov. All rights reserved.
//
#import "City.h"
@implementation City
- (id) init {
self = [super init];
if (self) {
self.population = arc4random() % 100;
}
return self;
}
- (void) setName:(NSString *)n {
name = n;
}
- (NSString*) getName {
return name;
}
- (void) setAge:(int)a {
age = a;
}
- (int) getAge {
return age;
}
- (void) setPopulation:(int)p {
population = p;
}
- (int) getPopulation {
return population;
}
- (void) nextDay {
population += arc4random() % population;
population -= arc4random() % population;
}
- (void) print {
NSLog(@"%@ (%i) %i", name, population, age);
}
@end
//
// main.m
// city
//
// Created by Kirill Klenov on 09.11.13.
// Copyright (c) 2013 Kirill Klenov. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "City.h"
#import "Metropolis.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
City *city;
city = [[City alloc] init];
[city setName:@"Default City"];
for (int i = 0; i < 10; i++) {
[city nextDay];
[city print];
};
Metropolis *metropolis = [[Metropolis alloc] init];
[metropolis createCity:@"City1" atIndex:0 withPopulation:100];
[metropolis createCity:@"City2" atIndex:1 withPopulation:110];
[metropolis createCity:@"City3" atIndex:2 withPopulation:120];
[metropolis createCity:@"City4" atIndex:3 withPopulation:130];
[metropolis createCity:@"City5" atIndex:4 withPopulation:140];
[metropolis createCity:@"City6" atIndex:5 withPopulation:150];
[metropolis createCity:@"City7" atIndex:6 withPopulation:160];
[metropolis createCity:@"City8" atIndex:7 withPopulation:170];
[metropolis createCity:@"City9" atIndex:8 withPopulation:180];
[metropolis createCity:@"City0" atIndex:9 withPopulation:190];
for (int i = 0; i < 10; i++) {
city = [metropolis getCities][i];
[city nextDay];
[city print];
}
}
return 0;
}
//
// Metropolis.h
// city
//
// Created by Kirill Klenov on 09.11.13.
// Copyright (c) 2013 Kirill Klenov. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "City.h"
@interface Metropolis : NSObject
- (City*) createCity:(NSString*)n atIndex:(int)i withPopulation:(int)p;
- (NSMutableArray*) getCities;
@end
//
// Metropolis.m
// city
//
// Created by Kirill Klenov on 09.11.13.
// Copyright (c) 2013 Kirill Klenov. All rights reserved.
//
#import "Metropolis.h"
#import "City.h"
@implementation Metropolis {
NSMutableArray *cities;
}
- (id)init {
self = [super init];
cities = [[NSMutableArray alloc] initWithCapacity:10];
return self;
}
- (NSMutableArray*) getCities {
return cities;
}
- (City*) createCity:(NSString*)n atIndex:(int)i withPopulation:(int)p {
City *city = [[City alloc] init];
[city setName:n];
[city setPopulation:p];
[cities insertObject:city atIndex:i];
return city;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment