Tuesday, March 12, 2013

Introduction to Core Data






Core Data: it is used to store data from your iPhone application into a Sqlite file which is present in the document directory of your application. Core Data is not a relational database, you can see Core data as a wrapper around Sqlite although core data is quite simpler as compared to Sqlite but it does not offer some of the functionality that Sqlite can offer and vice versa.



ManagedObject: Managed objects are the objects that are created by your application code to store data. A managed object can be thought of as a row or a record in a relational database table. For each new record to be added, a new managed object must be created to store the data. Similarly, retrieved data will be returned in the form of managed objects, one for each record matching the defined retrieval criteria. Managed objects are actually instances of the NSManagedObject class, or a subclass thereof. These objects are contained and maintained by the managed object context.



Persistence store coordinator: The persistent store coordinator is responsible for coordinating access to multiple persistent object stores. As an iPhone developer you will never directly interact with the persistence store coordinator and, in fact, will very rarely need to develop an application that requires more than one persistent object store. When multiple stores are required, the coordinator presents these stores to the upper layers of the Core Data stack as a single store.


Managed Object Context: Core Data based applications never interact directly with the persistent store. Instead, the application code interacts with the managed objects contained in the managed object context layer of the Core Data stack. The context maintains the status of the objects in relation to the underlying data store and manages the relationships between managed objects defined by the managed object model. All interactions with the underlying database are held temporarily in within the context until the context is instructed to save the changes, at which point the changes are passed down through the Core Data stack and written to the persistent store.

Source: http://qs4int.blogspot.in/2013/01/core-data.html
 

Thursday, March 07, 2013

Simple Example For Categories

NSDate+ComponentsExtractor.h:


 
typedef struct 
{
   NSInteger year;
   NSInteger month;
   NSInteger day;
} dateComponents;

@interface NSDate (ComponentsExtractor)
+ (dateComponents)componentsFromDate:(NSDate *)theDate;
@end
 
NSDate+ComponentsExtractor.m: 

#import "NSDate+ComponentsExtractor.h"

@implementation NSDate (ComponentsExtractor)

+ (dateComponents)componentsFromDate:(NSDate *)theDate
{
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:
                                            NSGregorianCalendar] autorelease];
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | 
                                  NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags 
                                          fromDate:theDate];
    
    dateComponents theComponents;
    theComponents.year = [components year];
    theComponents.month = [components month];
    theComponents.day = [components day];
    
    return theComponents;
}

@end

Making use of the category:
NSDate *theDate = [NSDate date]; // use the current date ...
dateComponents components = [NSDate componentsFromDate:theDate];
NSLog(@"%i - %i - %i", components.year, components.month, components.day);
 
 

Friday, March 01, 2013

Shortening URLs in iOS


NSString *url = @"http://www.example.com";
NSString *apiUrl = [NSString stringWithFormat:@"http://api.tr.im/v1/trim_simple?url=%@",url];
NSString *shortURL = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiUrl]
encoding:NSASCIIStringEncoding
error:nil];
NSLog(@"Long: %@ = Short: %@",url,shortURL);

Swift Operators - Basic Part 3 (Range operators in swift)

Range Operators: Closed Range operators  Half-Open Range Operators One-Sided Ranges Closed Range Operators:  a...b It defines...