Skip to content

Instantly share code, notes, and snippets.

@izzuddin91
Created September 19, 2017 05:00
Show Gist options
  • Select an option

  • Save izzuddin91/9d6ee0de590d103bbf46ffb3b5f624d8 to your computer and use it in GitHub Desktop.

Select an option

Save izzuddin91/9d6ee0de590d103bbf46ffb3b5f624d8 to your computer and use it in GitHub Desktop.
revision obj c until 19 sep 2017
//
// main.m
// objective-c relearn
//
// Created by izzuddin on 10/08/2017.
// Copyright © 2017 izzuddin. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <stdio.h>
#import "NSObject+BNREmployee.h"
#import "NSObject+BNRAsset.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// BNREmployee *ali = [[BNREmployee alloc] init];
// [ali setAge:23];
// [ali setHeight:170.2];
// [ali setWeight:60.6];
// [ali setHiredate: @"2017-01-01T08:39:57-08:00" ];
//
//// dateWithNaturalLanguageString - DEPRECIATED
//// [ali setHiredate:[ NSDate dateWithNaturalLanguageString: @"Aug 2nd, 2017" ]];
//
//
// NSDate *tarikhjoin = ali.tarikhmasuk;
//
// NSLog(@"ali join the company on %@", tarikhjoin );
//
// NSString *alitest = [ali testpad];
//
// NSLog(@" return test for ali is char is %@", alitest);
//
// NSLog(@"%@ hired on %@", ali, tarikhjoin);
//
// BNRPerson *abu = [[BNRPerson alloc] init];
// [abu setWeight:12.1];
// [abu setHeight:100.2];
// [abu setAge:22];
//
// BNRAsset *test = [[BNRAsset alloc] init];
// char *sen = [ test testFunction ];
//
// NSLog(@"test function %s", sen);
//
// NSString *abutest = [abu testpad];
// NSLog(@" return test for abu char is %@", abutest);
//
// BNRAsset *aset = [[BNRAsset alloc] init];
//
// [aset setLabel:@"LABEL"];
// [aset setResaleValue:123];
//
// NSLog(@"test dealloc %@ ", aset);
//
//create an array of BNREmployee objects
NSMutableArray *employees = [[NSMutableArray alloc] init];
NSMutableDictionary *execs = [[NSMutableDictionary alloc] init];
for (int i = 0; i < 10 ; i++){
//create an instances of BNREmployee
BNREmployee *mikey = [[BNREmployee alloc] init];
// mikey.age = 25;
mikey.weight = i + 60 ;
mikey.employeeID = i;
[employees addObject:mikey];
//check if its the first employee
if (i == 0){
[execs setObject:mikey forKey:@"CEO"];
}
if (i == 1){
[execs setObject:mikey forKey:@"CTO"];
}
}
NSMutableArray *allAssets = [[NSMutableArray alloc] init];
//create 10 assets
for ( int i = 0; i < 10 ; i++){
//create an instances of assets
BNRAsset *asset = [ [BNRAsset alloc] init];
NSString *currentlabel = [ NSString stringWithFormat: @"Laptop %d", i];
asset.label = currentlabel;
asset.resaleValue = 350 * i * 17;
//get a random number between 0 and 8 inclusive;
NSUInteger randomIndex = random() % [employees count];
//find that employee
BNREmployee *employee = [employees objectAtIndex:randomIndex];
//assign that assets to the employee
[employee addAssets: asset];
[allAssets addObject:asset];
}
BNREmployee *firstEmployee = [employees objectAtIndex:1];
NSSet *firstEmployeeArray = [firstEmployee assets];
BNRAsset *firstAsset = [allAssets objectAtIndex: 9];
int verdict = [firstEmployeeArray containsObject:firstAsset];
NSLog(@"verdict :%d", verdict );
//sorting
NSSortDescriptor *voa = [NSSortDescriptor sortDescriptorWithKey:@"valueOfAssets" ascending:YES];
NSSortDescriptor *eid = [NSSortDescriptor sortDescriptorWithKey:@"employeeID" ascending:YES];
[employees sortUsingDescriptors:@[voa, eid]];
NSLog(@"Employee: %@", employees);
//filtering
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"holder.valueOfAssets > 70"];
NSArray *toBeClaimed = [allAssets filteredArrayUsingPredicate:predicate];
NSLog(@"to be claimed: %@", toBeClaimed);
toBeClaimed = nil;
NSLog(@"Giving up ownership of one employee");
[employees removeObjectAtIndex:5];
NSLog(@"all assets: %@", allAssets);
NSLog(@"giving up ownership of arrays");
employees = nil ;
NSLog(@"execs %@", execs);
NSLog(@"CEOadasdasd: %@", execs[@"CEO"]);
execs = nil;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"hola" forKey:@"helo"];
NSString *greeting = [dict objectForKey:@"helo"];
// [dict setObject:@"abu" forKey:@"helo"];
NSMutableArray *list = [[NSMutableArray alloc] init];
[list addObject:@4];
[list addObject:@5.6];
NSPoint somePoint = NSMakePoint(100, 100);
NSValue *pointValue = [ NSValue valueWithPoint:somePoint ];
[list addObject:pointValue];
NSLog(@"\u03c0 is %f", M_PI);
NSLog(@"%d is larger", MAX(10, 12));
NSLocale *here = [NSLocale currentLocale];
NSString *currency = [here objectForKey:NSLocaleCurrencyCode];
NSLog(@"currency is %@", currency);
int number = BlendSpeedStir;
NSLog(@"asdasdasdasdasdasddasdasdasdas%d", number);
}
return 0;
}
//
// NSObject+BNRAsset.h
// objective-c relearn
//
// Created by izzuddin on 25/08/2017.
// Copyright © 2017 izzuddin. All rights reserved.
//
#import <Foundation/Foundation.h>
@class BNREmployee;
@interface BNRAsset : NSObject
@property (nonatomic, copy) NSString *label;
@property (nonatomic) unsigned int resaleValue;
// use weak reference to make sure smooth deallocation
@property (nonatomic, weak) BNREmployee *holder;
- (char *)testFunction;
@end
//
// NSObject+BNRAsset.m
// objective-c relearn
//
// Created by izzuddin on 25/08/2017.
// Copyright © 2017 izzuddin. All rights reserved.
//
#import "NSObject+BNRAsset.h"
#import "NSObject+BNREmployee.h"
@implementation BNRAsset : NSObject
- (char *)testFunction {
return "hello world!\n";
}
- (NSString *)description{
// return [ NSString stringWithFormat:@"<%@: $%u>", self.label, self.resaleValue ];
if (self.holder){
return [ NSString stringWithFormat:@"<%@: $%u, assigned to %@>", self.label, self.resaleValue, self.holder ];
}else{
return [ NSString stringWithFormat:@"<%@: $%u>", self.label, self.resaleValue ];
}
}
- (void)dealloc{
NSLog(@"deallocating %@", self);
}
@end
//
// NSObject+BNREmployee.h
// objective-c relearn
//
// Created by izzuddin on 17/08/2017.
// Copyright © 2017 izzuddin. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "NSObject+BNRPerson.h"
@class BNRAsset;
@interface BNREmployee : BNRPerson
//class extension pindah ke BNREmployee.m
//{
// NSMutableArray *_assets;
//}
@property (nonatomic) unsigned int employeeID;
//@property (nonatomic) unsigned int officeAlarmCode;
@property (nonatomic) NSDate *tarikhmasuk;
@property (nonatomic) NSString *lastname;
@property (nonatomic) BNRPerson *spouse;
@property (nonatomic) NSMutableArray * children;
@property (nonatomic, copy) NSSet *assets;
- (void)setHiredate:(NSString *)hiredate;
- (NSString *)testpad;
- (NSString *)description;
- (double)yearsOfEmployment;
- (void)addAssets:(BNRAsset *)a;
- (unsigned int)valueOfAssets;
//- (NSDate *)sethiredate;
//{
// unsigned int _employeeID;
// unsigned int _officeAlarmCode;
// NSDate *_hireDate;
//}
//
//- (unsigned int)employeeID;
//- (unsigned int)officeAlarmCode;
//- (void)setHireDate: (NSDate *)hiredate;
//- (NSDate *)hireDate;
@end
//
// NSObject+BNREmployee.m
// objective-c relearn
//
// Created by izzuddin on 17/08/2017.
// Copyright © 2017 izzuddin. All rights reserved.
//
#import "NSObject+BNREmployee.h"
//THIS IS A CLASS EXTENSION
@interface BNREmployee ()
{
NSMutableSet *_assets;
}
@property (nonatomic) unsigned int officeAlarmCode;
@end
#import "NSObject+BNRAsset.h"
//extension
@implementation BNREmployee : BNRPerson
- (void)setAssets:(NSArray *)assets{
_assets = [assets mutableCopy];
}
- (NSArray *)assets{
return [ _assets copy];
}
- (void)addAssets:(BNRAsset *)a{
if (!_assets){
_assets = [ [NSMutableSet alloc] init ];
}
[_assets addObject: a];
a.holder = self;
}
- (unsigned int)valueOfAssets{
//sum up the resale value of the assets
unsigned int sum = 0;
for (BNRAsset *a in _assets){
sum += [a resaleValue];
}
return sum;
}
- (void)setHiredate:(NSString *)hiredate{
NSDateFormatter *date = [ [NSDateFormatter alloc] init ];
date.locale = [ NSLocale localeWithLocaleIdentifier:@"en_US_POSIX" ];
date.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
date.timeZone = [ NSTimeZone timeZoneForSecondsFromGMT:0 ];
NSDate *enterdate = [date dateFromString: hiredate];
_tarikhmasuk = enterdate;
}
- (NSString *)testpad{
return @"xyz";
}
- (NSString *)description{
// return [ NSString stringWithFormat:@"<Employee %d>", self.employeeID ];
return [NSString stringWithFormat:@"<Employee %u: $%u in assets>", self.employeeID, self.valueOfAssets];
}
- (void)dealloc{
NSLog(@"deallocating %@", self);
}
//- (unsigned int)employeeID{
// return _employeeID;
//}
//
//
//- (unsigned int)officeAlarmCode{
// return _officeAlarmCode;
//}
//
//
//- (void)setHireDate: hiredate{
// _hireDate = hiredate;
//}
//
//- (NSDate *)hireDate{
// return _hireDate;
//}
//
//
- (double)yearsOfEmployment{
NSDate *date = [NSDate date];
if (self.tarikhmasuk){
NSTimeInterval secs = [date timeIntervalSinceDate: self.tarikhmasuk ];
return secs / 31557600.0;
}else{
return 0;
}
}
@end
//
// NSObject+BNRPerson.h
// objective-c relearn
//
// Created by izzuddin on 17/08/2017.
// Copyright © 2017 izzuddin. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BNRPerson : NSObject
//{
// float _weight;
// float _height;
// int _age;
//}
//typedef enum
//{
// Atype = 1,
// Btype,
// Ctype,
// Dtype,
// Etype,
//
//}type;
typedef NS_ENUM(char, BlendSpeed){
BlendSpeedStir,
BlendSpeedChop
};
@property (nonatomic, readwrite) float weight;
@property (nonatomic, readwrite) float height;
@property (nonatomic) int year;
//- (float)getWeight;
//- (float)getHeight;
//- (int)getAge;
//
//- (void)setWeight: (float)w;
//- (void)setHeight: (float)h;
//- (void)setAge: (int)a;
- (NSString *)testpad;
@end
//
// NSObject+BNRPerson.m
// objective-c relearn
//
// Created by izzuddin on 17/08/2017.
// Copyright © 2017 izzuddin. All rights reserved.
//
#import "NSObject+BNRPerson.h"
@interface BNRPerson ()
@property (nonatomic) int age;
@end
@implementation BNRPerson : NSObject
//- (float)getWeight{
// return _weight;
//}
//
//- (float)getHeight{
// return _height;
//}
//
//- (int)getAge{
// return _age;
//}
//
//- (void)setWeight:(float)w{
// _weight = w;
//}
//
//- (void)setHeight:(float)h{
// _height = h;
//}
//
//- (void)setAge:(int)a{
// _age = a;
//}
- (NSString *)testpad{
NSString *testchar = @"abc";
return testchar;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment