+ class method- instance methodThat is the convention for naming files, that contain categories for existing classes. Categories are a way of adding methods to an existing class in Objective-C, without subclassing
-(UIColor *)colorFromHex:(NSString *)hex {
unsigned int c;
if ([hex characterAtIndex:0] == '#') {
[[NSScanner scannerWithString:[hex substringFromIndex:1]] scanHexInt:&c];
} else {
[[NSScanner scannerWithString:hex] scanHexInt:&c];
}
return [UIColor colorWithRed:((c & 0xff0000) >> 16)/255.0
green:((c & 0xff00) >> 8)/255.0
blue:(c & 0xff)/255.0 alpha:1.0];
}
//code here
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[NSTimer scheduledTimerWithTimeInterval:60.0
target:self
selector:@selector(someMethod)
userInfo:nil
repeats:YES];
}
- (void) someMethod {
NSLog(@"hello I am called");
}
NSString *prefix=@"http://ec2-54-251-248-32.ap-southeast-1.compute.amazonaws.com/backup/db/samquery1.php?userid=";
NSInteger uid = 100; //let's say user id is 111
NSString *postfix = [NSString stringWithFormat:@"%ld", (long)uid];
NSString *qstring = [ prefix stringByAppendingString:postfix];
NSURL *url=[NSURL URLWithString:qstring];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error=nil;
NSArray *jsonArray =[NSJSONSerialization JSONObjectWithData:data options:
NSJSONReadingMutableContainers error:&error];
if (!jsonArray) {
NSLog(@"Error parsing JSON: %@", error);
} else {
for(NSDictionary *item in jsonArray) {
//NSLog(@"Item: %@", item);
//NSString *negemo = [item objectForKey:@"negemo"];
//NSLog(@"negative emo is %@", negemo);
NSLog(@"=====start of row=====\n");
NSArray *keys = [item allKeys];
for (id key in keys)
{
id aValue = [item objectForKey:key];
NSLog(@"key is %@ and value is %@",key,aValue);
}
NSLog(@"=====end of row=====\n");
}
}
NSArray* arr = [[NSArray alloc]initWithObjects:@"work",@"email",@"dreamon",@"aaa",@"bbb",@"ccc", nil];
NSMutableArray *randomSelection = [[NSMutableArray alloc]init];
[randomSelection addObject:@"string1"];
//NSString is immutable , it equals to constant NSMutableString
//NSMutableString is mutable
NSArray *monthlySymbols = [[[NSDateFormatter alloc] init] shortMonthSymbols];
NSDate *now = [NSDate date];
NSDate *seventyDaysAgo = [now dateByAddingTimeInterval:-356*24*60*60];
//NSLog(@"70 days ago: %@", seventyDaysAgo);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
//NSString *MyString = [dateFormatter stringFromDate:seventyDaysAgo];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *oneDay = [[NSDateComponents alloc] init];
[oneDay setDay: 1];
for (NSDate* date = seventyDaysAgo; [date compare: now] <= 0;
date = [calendar dateByAddingComponents: oneDay
toDate: date
options: 0] ) {
//NSLog( @"%@ in [%@,%@]", date, sevenDaysAgo, now );
NSLog(@"date is %@", [dateFormatter stringFromDate:date]);
}
NSDate *now = [NSDate date];
NSDate *sevenDaysAgo = [now dateByAddingTimeInterval:-7*24*60*60];
NSLog(@"7 days ago: %@", sevenDaysAgo);
[reference](http://stackoverflow.com/questions/8927727/objective-c-arc-strong-vs-retain-and-weak-vs-assign(
- (BOOL)isLandscape
{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == 0 || orientation == UIInterfaceOrientationPortrait)
return NO;
else
return YES;
//return UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation); //this is not accurate some times
}
http://stackoverflow.com/questions/3904663/what-does-class-do-in-objective-c