Skip to content

Instantly share code, notes, and snippets.

@sodastsai
Created November 8, 2012 01:14
Show Gist options
  • Select an option

  • Save sodastsai/4035770 to your computer and use it in GitHub Desktop.

Select an option

Save sodastsai/4035770 to your computer and use it in GitHub Desktop.
11/08 Code snippets
DMAuthor *author1 = [DMAuthor authorWithFirstName:@"Jo" lastName:@"Walton"];
DMAuthor *author2 = [DMAuthor authorWithFirstName:@"Connie" lastName:@"Willis"];
DMAuthor *author3 = [DMAuthor authorWithFirstName:@"J.K." lastName:@"Rowling"];
DMBook *book1 = [[DMBook alloc] initWithTitle:@"Among Others" subtitle:nil price:10.19 andAuthor:author1];
DMBook *book2 = [[DMBook alloc] initWithTitle:@"Blackout" subtitle:nil price:11.68 andAuthor:author2];
DMBook *book3 = [[DMBook alloc] initWithTitle:@"Harry Potter with Philisopher's Stone" subtitle:nil price:10.0f andAuthor:author3];
DMBook *book4 = [[DMBook alloc] initWithTitle:@"Harry Potter with Chamber of Secrets" subtitle:nil price:12.0f andAuthor:author3];
DMBook *book5 = [[DMBook alloc] initWithTitle:@"Harry Potter with Prisoner of Azkaban" subtitle:nil price:14.0f andAuthor:author3];
NSArray *authors = @[author1, author2, author3];
NSArray *books = @[book1, book2, book3, book4, book5];
//
// DMBook.h
// demo
//
// Created by sodas on 11/8/12.
// Copyright (c) 2012 NTU Mobile HCI Lab. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DMAuthor : NSObject
@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;
@property (nonatomic, readonly) NSString *fullName;
+ (id)authorWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;
- (id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;
@end
@interface DMBook : NSObject
@property (nonatomic, weak) DMAuthor *author;
@property (nonatomic) float price;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
+ (id)bookWithTitle:(NSString *)title;
- (id)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle price:(float)price andAuthor:(DMAuthor *)author;
@end
//
// DMBook.m
// demo
//
// Created by sodas on 11/8/12.
// Copyright (c) 2012 NTU Mobile HCI Lab. All rights reserved.
//
#import "DMBook.h"
@implementation DMAuthor
- (NSString *)fullName {
return [NSString stringWithFormat:@"%@ %@",
self.firstName, self.lastName];
}
+ (id)authorWithFirstName:(NSString *)firstName lastName:(NSString *)lastName {
return [[self alloc] initWithFirstName:firstName lastName:lastName];
}
- (id)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName {
if (self = [super init]) {
_firstName = firstName;
_lastName = lastName;
}
return self;
}
@end
@implementation DMBook
+ (id)bookWithTitle:(NSString *)title {
return [[self alloc] initWithTitle:title
subtitle:nil
price:0.0f
andAuthor:nil];
}
- (id)initWithTitle:(NSString *)title subtitle:(NSString *)subtitle price:(float)price andAuthor:(DMAuthor *)author {
if (self = [super init]) {
_title = title;
_subtitle = subtitle;
_price = price;
_author = author;
}
return self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment