Skip to content

Instantly share code, notes, and snippets.

@waitonza
Created January 31, 2014 16:43
Show Gist options
  • Save waitonza/8735903 to your computer and use it in GitHub Desktop.
Save waitonza/8735903 to your computer and use it in GitHub Desktop.
Basic OOP in Objective-C
//
// Classroom.h
// SocketTest
//
// Created by Wairung Tiranalinvit on 1/31/2557 BE.
// Copyright (c) 2557 Wairung Tiranalinvit. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Student.h"
@interface Classroom : NSObject
- (void)addStudent:(Student*)student;
- (void)addStudentWithID:(NSString*)sid andName:(NSString*)name;
- (NSArray*)students;
@end
//
// Classroom.m
// SocketTest
//
// Created by Wairung Tiranalinvit on 1/31/2557 BE.
// Copyright (c) 2557 Wairung Tiranalinvit. All rights reserved.
//
#import "Classroom.h"
#import "Student.h"
@implementation Classroom {
NSMutableArray *students;
}
- (id)init
{
self = [super init];
if (self) {
Student *ton = [[Student alloc] init];
ton.studentID = @"A1";
ton.studentName = @"TON";
Student *test = [[Student alloc] init];
test.studentID = @"A2";
test.studentName = @"TEST";
students = [[NSMutableArray alloc] initWithArray:@[ton, test]];
}
return self;
}
- (void)addStudent:(Student*)student
{
[students addObject:student];
}
- (void)addStudentWithID:(NSString*)sid andName:(NSString*)name
{
Student *student = [[Student alloc] init];
student.studentID = sid;
student.studentName = name;
[students addObject:student];
}
- (NSArray*)students
{
return students;
}
@end
//
// Student.h
// SocketTest
//
// Created by Wairung Tiranalinvit on 1/31/2557 BE.
// Copyright (c) 2557 Wairung Tiranalinvit. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface Student : NSObject
@property (nonatomic, strong) NSString *studentID;
@property (nonatomic, strong) NSString *studentName;
@property (nonatomic) float currentGrade;
@end
//
// Student.m
// SocketTest
//
// Created by Wairung Tiranalinvit on 1/31/2557 BE.
// Copyright (c) 2557 Wairung Tiranalinvit. All rights reserved.
//
#import "Student.h"
@implementation Student
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment