Skip to content

Instantly share code, notes, and snippets.

@LeffelMania
Last active August 29, 2015 14:00
Show Gist options
  • Save LeffelMania/11017206 to your computer and use it in GitHub Desktop.
Save LeffelMania/11017206 to your computer and use it in GitHub Desktop.
iOS Refactor
// Create a new command-line project in Xcode by going to
// File -> New -> Project
// Under "OS X" select "Application" and "Command Line Tool"
// Name the product whatever you want and set the type to "Foundation"
// Replace the created main.m file with this file
#import <Foundation/Foundation.h>
@interface Email : NSObject
@property (nonatomic, strong) NSString *e;
- (id)initWithAddress:(NSString *)email;
- (void)sendEmail:(NSString *)body;
@end
@implementation Email
- (id)initWithAddress:(NSString *)email
{
if (self = [super init]) {
_e = email;
}
return self;
}
- (void)sendEmail:(NSString *)body
{
NSLog(@"Email sent to %@", _e);
}
@end
@interface Phone : NSObject
@property (nonatomic, strong) NSString *n;
- (id)init:(NSString*)num;
- (void)sendSms:(NSString *)m;
@end
@implementation Phone
- (id)init:(NSString *)num
{
if (self = [super init]){
_n = num;
}
return self;
}
- (void)sendSms:(NSString *)m
{
NSLog(@"SMS sent to %@", _n);
}
@end
@interface Message : NSObject
@property (nonatomic, strong) NSString *mBody;
- (id)initWithBody:(NSString *)body;
- (void)send:(NSArray *)recipients;
@end
@implementation Message
- (id)initWithBody:(NSString *)body
{
if (self = [super init])
{
_mBody = body;
}
return self;
}
- (void)send:(NSArray *)recipients
{
if ([self check])
{
NSLog(@"Sending '%@'", _mBody);
for (int i = 0; i < recipients.count; i++){
id obj = [recipients objectAtIndex:i];
if ([obj isKindOfClass:[Phone class]]){
[((Phone *)obj) sendSms:_mBody];
}
else if([obj isKindOfClass:[Email class]]){
[((Email*)obj) sendEmail:_mBody];
}
else{
NSLog(@"Couldn't send to %@", obj);
}
}
}
}
- (BOOL)check
{
return _mBody.length;
}
@end
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray *recipients = [[NSArray alloc] initWithObjects:
[[Phone alloc] init:@"8675309"],
[[Email alloc] initWithAddress:@"jenny@tutone.net"],
@"tommy@tutone.net",
nil];
[[[Message alloc] initWithBody:@"Who can I turn to?"] send:recipients];
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment