By A Beginner

Every Expert Was Once A Beginner

iOS:To seprate group of ANAGRAMS in an array — January 17, 2015

iOS:To seprate group of ANAGRAMS in an array

Here is the code for grouping and printing the set of anagrams in an array.If any better solutions please Comment.


- (void)viewDidLoad
 {
 [super viewDidLoad];
 //self.view.backgroundColor=[UIColor blueColor];
 // Do any additional setup after loading the view, typically from a nib.
 NSString *tmpStr1;
 NSString *tmpStr2;
 NSMutableArray *newArr=[[NSMutableArray alloc] init];
 BOOL Var=NO;
 int i,j;
 NSArray *arr1=[[NSArray alloc] initWithObjects:@"star",
 @"rats",
 @"arts",
 @"car",
 @"rac",
 @"arc",
 @"abcde",
 @"bcdea",
 @"cdeab", nil];
 for (i=0; i<[arr1 count]; i++) {
 for (j=i+1; j<[arr1 count]; j++) {
 tmpStr1=[arr1 objectAtIndex: i]; //star
 tmpStr2=[arr1 objectAtIndex:j]; //rats
 if ( tmpStr1.length == tmpStr2.length) { //4==4
 [newArr addObject:tmpStr1]; //newArray=[star]
 for (int k=0; k<tmpStr2.length; k++) {
 //Get the letter by letter in tmpStr2
 NSString cmpStr=[NSString stringWithFormat:@"%C",[tmpStr2 characterAtIndex:k]]];
 if ([tmpStr1 rangeOfString:cmpStr.location == NSNotFound) {
 Var=NO; //Characters in tmpStr2 not found in tmpStr1
 } else {
 Var=YES; //Characters in tmpStr2 found in tmpStr1
 }
 }
 //if all the letters in string tmpStr2 are present in string tmpStr1
 if (Var) { 
 //add the second string i.e. tmpStr2 to the array newArr
 [newArr addObject:tmpStr2]; 
 }
 }
 else
 {
 i=j; 
 //remove the duplicate strings in the array newArr
 //print the anagram set to log
 NSLog(@"NewArr::>%@",[[NSSet setWithArray:newArr] allObjects]); 
 //remove the objects in the array for the next set of anagrams
 [newArr removeAllObjects]; 
 //Controll goes to this line::> for (j=i+1; j<[arr1 count]; j++)
 continue; 
 }
 }
 }
 NSLog(@"Arr2::%@",[[NSSet setWithArray:newArr] allObjects]);
 NSLog(@"Arr1::%@",arr1); //To print the original array
 }



Find on Git: Here!
iOS:Database in applications usingFMDB Wrapper Class —

iOS:Database in applications usingFMDB Wrapper Class

  We can include the database in our iPad/iPhone applications with the help of   SQLite and FMDB wrapper class. Here is how we can achieve it with following simple steps.

1. First with the help of Mozilla Firefox SQLite plug-in create a MyDatabase.sqlite file and save it in your project folder.

2.Get the FMDB Wrapper classes from the git hub project  https://github.com/ccgus/fmdb unzip it and navigate to the fmdb folder (fmdb-master.zip\fmdb-master\src\fmdb) and copy this to your Xcode project.

 3. Write this piece of code in your appdelegate.m  in   file to make a connection to your MyDatabase.sqlite file  and save the path to your database in NSUserDefaults so that we can use it when we are doing any DB operations.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

//-------------------DataBase connection----------------------

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSError *error;

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];



    // To Get the path of the database file during runtime of application

    NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"MYDatabase.sqlite"];

    NSLog(@"Database path: %@",databasePath);



    if ([fileManager fileExistsAtPath:databasePath] == NO) {

        NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"MYDatabase" ofType:@"sqlite"];

        [fileManager copyItemAtPath:resourcePath toPath:databasePath error:&error];

    }

    //save the database path in userdefaults  so that you can use it for connecting to database and making database operations

    [[NSUserDefaults standardUserDefaults] setValue:databasePath forKey:dbPath];

    [[NSUserDefaults standardUserDefaults] synchronize];

}

4. In ViewController.m file of your project import the two header files

#import "FMDatabase.h"
#import "FMResultSet.h"

then write the following code to connect to the database and make the insertion and selection operations in your app Database.

- (void)viewDidLoad
{

[self insertDataInDB];

//array  dbResultArray Has the resultant rows retrieved from db by SELECT Query

NSArray *dbResultArray=[NSArray arrayWithArray:[self getDataFromDB]] ;

//To store the data retrieved in a variables for use 

//Use your convenient way to store the  retrieved  data  and use it in your app

for(int i=0 ; i<dbResultArray.count;i++){
  NSString *str1=[[dbResultArray objectAtIndex:i] valueForKey:@"column1Value"];
  NSString *str2=[[dbResultArray objectAtIndex:i] valueForKey:@"column2Value"];  
.
.  
.
  NSString *strN=[[dbResultArray objectAtIndex:i] valueForKey:@"columnNValue"];
}//String 
}

This below method inserts the data in to database :

-(void)insertDataInDB
{
//Connec to database
NSString *dbPath=[[NSUserDefaults standardUserDefaults]valueForKey:dbPath];
FMDatabase *localDB = [FMDatabase databaseWithPath:dbPath];
[localDB open];
NSString *queryStr;

queryStr=[NSString stringWithFormat:@"INSERT INTO table_name (Collumn1,Collumn2,Collumn3,Collumn4,....,CollumnN) VALUES (Value1,'Value2','Value3','Value4',.....,'ValueN')"];

  if (![localDB executeUpdate:queryStr]) {

          //NSLog(@"failed Inserting data to DB: %@", [localDB lastErrorMessage]);

      }
 [localDB close];

}

This below method  returns the array of Dictionary containing the Retrieved data from database:

-(NSMutableArray *)getDataFromDB
{
//Connec to database
NSString *dbPath=[[NSUserDefaults standardUserDefaults]valueForKey:dbPath];
FMDatabase *localDB = [FMDatabase databaseWithPath:dbPath];
[localDB open];
NSMutableArray *tempArrayV=[[NSMutableArray alloc]init];     
NSString *queryStr=[NSString stringWithFormat:@"SELECT * from table_name"];
FMResultSet *resultQuery = [localDB executeQuery:queryStr];
//After the query is executed 
while ([resultQuery next]) {
NSDictionary * DataDict =[NSDictionary dictionaryWithObjectsAndKeys:
[resultQuery stringForColumn:@"Column1"],@"column1Value",
[resultQuery stringForColumn:@"Column2"],@"column2Value",
[resultQuery stringForColumn:@"Column3"],@"column3Value",
.
.
.
[resultQuery stringForColumn:@"ColumnN"],@"columnNValue",nil];
[tempArrayV addObject:DataDict];
}
return tempArrayV;
[localDB close];
}

Hope this will help you to implement the Application with the database support.

iOS – Architecture — January 14, 2015

iOS – Architecture

                   
                iOS is an operating system for the iPhone’s (an Apple Inc Product). It is derived from core OS X technologies ( an OS for MAC pc) .The iOS includes the OS X Kernel , BSD Sockets for Networking  and objective-C and C/C++ compilers for native performance. iOS 7.0 is the latest release of iOS on June 10 2013.


The iOS has the layered architecture, different layers are in the below figure.

Cocoa Touch is a UI framework for building software programs to run on the iOS operating system (for the iPhone, iPod Touch, and iPad) from Apple Inc.
Cocoa Touch mainly contains the classes implemented  in Objective-C, an object-oriented language that is compiled to run at incredible speed, yet employs a truly dynamic runtime making it uniquely flexible. Because Objective-C is a superset of C, it is easy to mix C and even C++ into your Cocoa Touch applications.
The framework’s of this layer provides provide the basic  app infrastructure and support for the key technologies ( .i.e Multitasking ,touch-based input, push notification and many  high level system services.)
UIKit Framework: This frame work provides the crucial infrastructure for implementing graphical ,event driven, apps in iOS.
The Media layer contains the graphics, audio, and video technologies you use to implement multimedia experiences in your apps. The technologies in this layer make it easy for you to build apps that look and sound great.
The technologies of this layer are listed as below:


Core Services Layer:

This Layer contains basic system services for apps. Core Foundation and Foundation Frameworks are the key services provided by this layer, which define the basic types that all apps use it also has the technologies which supports the features like location, iCloud,social media and networking.
Some of the High-Level Features and Frameworks provided by this layer are:

This layer contains the low-level features on basis of which the other high-level features are built.Even though we do not use any of these features directly in our  apps ,they used by the frameworks which are used by our application.We can make use of these features when we need to implement the security features  or communicating with an external hardware accessory.
Frameworks provided by this layer are: