Monday 5 March 2012

How To Work With GIT Hub

cat ~/.gitconfig
git clone git@github.com:bestbuymobileapps/bby-core-iphone-app.git
git fetch
git branch -all
git branch -r
git branch
git status
git pull origin BRANCH_NAME
git add .
git commit -m"message"
git push origin BRANCH_NAME
git log -7 --oneline
git checkout BRANCH_NAME
git log
ls
git branch -d dev-webcommerce
git merge -m"Merge Message" dev-webcommerce
git diff .
git branch BRANCH_NAME
git checkout -- Folder/Commit ID
git config --global push.default current
git reset --hardclear
git reset --hard
git rm filename
git log --oneline
pwd
clear
gitk
git stash
git revert commitID

How Manage Data In Objective-C

%@ Object
%d, %i signed int
%u unsigned int
%f float/double
%x, %X hexadecimal int
%o octal int
%zu size_t
%p pointer
%e float/double (in scientific notation)
%g float/double (as %f or %e, depending on value)
%s C string (bytes)
%S C string (unichar)
%.*s Pascal string (requires two arguments, pass pstr[0] as the first, pstr+1 as the second)
%c character
%C unichar
%lld long long
%llu unsigned long long
%Lf long double

Tuesday 28 February 2012

TextField Allow Only Numeric Value And The Text Length

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
BOOL allowChange = YES;
if ([textField isEqual:zipText])
{
allowChange = NO;
int textLength = [textField.text length];
int value;
if (([string isEqualToString:@""]))
{
allowChange=YES;
}
else
{
value = (int) [string characterAtIndex:0];
}
if ((textLength <= kMaxZipLength) && value >=48 && value <=57)
{
allowChange=YES;
}
}

return allowChange;
}

Monday 14 November 2011

How To Remove and Replace String within a String.

// Here We Deleting Something
NSString *content = @"foo/bar:baz.foo";
NSCharacterSet *deleteString = [NSCharacterSet characterSetWithCharactersInString:@"/:."];
content = [[content componentsSeparatedByCharactersInSet: deleteString] componentsJoinedByString: @""];
NSLog(@"String===>>>%@", content);

// Here We Replace Something
NSString *replaceString = @"This is a Dog";
replaceString = [replaceString stringByReplacingOccurrencesOfString:@"Dog" withString:@"Man"];
NSLog(@"Replace String===>>>%@", replaceString);

Friday 11 November 2011

How To Copying A String

NSMutableString *string1;
NSMutableString *string2;
string1 = [NSMutableString stringWithString: @"This is a string"];
string2 = string1;

How To Append A String Within A String

NSMutableString *string1;
NSMutableString *string2;
string1 = [NSMutableString stringWithString: @"This is a string"];
string2 = string1;
[string2 appendString: @" and it is mine!"];
NSLog (@"string1 = %@", string1);
NSLog (@"string2 = %@", string2);

How To Print The URl On The UIWebView

NSURL *url =[request URL];
NSString *urlStr =[url absoluteString];
NSLog(@"URL shouldStartLoadWithRequest===>>>%@",urlStr);

NSURL *url =[[webView request]URL];
NSString *urlStr =[url absoluteString];
NSLog(@"webViewDidStartLoad===>>>%@",urlStr);

NSURL *url =[[webView request]URL];
NSString *urlStr =[url absoluteString];
NSLog(@"webViewDidFinishLoad===>>>%@",urlStr);

How To Find The Current Date

NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
[formatter setDateFormat:@"HH:MM:SS"];
NSString* str =[formatter stringFromDate:date];
NSLog(str);
[timeLable setText:str];

Thursday 25 August 2011

Creating SQLite Data Base For iPhone Apps.

#import
#import

@interface todoAppDelegate : NSObject {

IBOutlet UIWindow *window;
IBOutlet UINavigationController *navigationController;

sqlite3 *database;
NSMutableArray *todos;
}

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *todos;

@end


#import "todoAppDelegate.h"
#import "RootViewController.h"
#import "Todo.h"

@interface todoAppDelegate (Private)
- (void)createEditableCopyOfDatabaseIfNeeded;
- (void)initializeDatabase;
@end

@implementation todoAppDelegate

@synthesize window;
@synthesize navigationController;
@synthesize todos;

- (id)init {
if (self = [super init]) {
//
}
return self;
}


- (void)applicationDidFinishLaunching:(UIApplication *)application {

[self createEditableCopyOfDatabaseIfNeeded];
[self initializeDatabase];

// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}

// Creates a writable copy of the bundled default database in the application Documents directory.
- (void)createEditableCopyOfDatabaseIfNeeded {

// First, test for existence.
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"todo.sqlite"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"todo.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {

NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}

// Open the database connection and retrieve minimal information for all objects.
- (void)initializeDatabase {

NSMutableArray *todoArray = [[NSMutableArray alloc] init];
self.todos = todoArray;
[todoArray release];
// The database is stored in the application bundle.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"todo.sqlite"];
// Open the database. The database was prepared outside the application.
if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {
// Get the primary key for all books.
const char *sql = "SELECT pk FROM todo";
sqlite3_stmt *statement;
// Preparing a statement compiles the SQL query into a byte-code program in the SQLite library.
// The third parameter is either the length of the SQL string or -1 to read up to the first null terminator.
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
// We "step" through the results - once for each row.
while (sqlite3_step(statement) == SQLITE_ROW) {
// The second parameter indicates the column index into the result set.
int primaryKey = sqlite3_column_int(statement, 0);
// We avoid the alloc-init-autorelease pattern here because we are in a tight loop and
// autorelease is slightly more expensive than release. This design choice has nothing to do with
// actual memory management - at the end of this block of code, all the book objects allocated
// here will be in memory regardless of whether we use autorelease or release, because they are
// retained by the books array.
Todo *td = [[Todo alloc] initWithPrimaryKey:primaryKey database:database];
[todos addObject:td];
[td release];
}
}
// "Finalize" the statement - releases the resources associated with the statement.
sqlite3_finalize(statement);
} else {
// Even though the open failed, call close to properly clean up resources.
sqlite3_close(database);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
// Additional error handling, as appropriate...
}

//NSLog([[NSString alloc] initWithFormat:@"size:%@",[[todos objectAtIndex:1] text]]);
}


- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate
}


- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}

@end


#import

@interface RootViewController : UITableViewController {

}

@end


#import "RootViewController.h"


@implementation RootViewController


- (void)viewDidLoad {

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"MyIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
}

// Set up the cell
return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

// Navigation logic
}
- (void)viewWillAppear:(BOOL)animated {

[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
}

- (void)viewDidDisappear:(BOOL)animated {
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}


- (void)dealloc {

[super dealloc];
}


@end


#import
#import

@interface Todo : NSObject {
sqlite3 *database;
NSInteger primaryKey;
NSString *text;
}

@property (assign, nonatomic, readonly) NSInteger primaryKey;
@property (nonatomic, retain) NSString *text;

- (id)initWithPrimaryKey:(NSInteger)pk database:(sqlite3 *)db;

@end


#import "Todo.h"

static sqlite3_stmt *init_statement = nil;

@implementation Todo
@synthesize primaryKey,text;

- (id)initWithPrimaryKey:(NSInteger)pk database:(sqlite3 *)db {

if (self = [super init]) {

primaryKey = pk;
database = db;
// Compile the query for retrieving book data. See insertNewBookIntoDatabase: for more detail.
if (init_statement == nil) {
// Note the '?' at the end of the query. This is a parameter which can be replaced by a bound variable.
// This is a great way to optimize because frequently used queries can be compiled once, then with each
// use new variable values can be bound to placeholders.
const char *sql = "SELECT text FROM todo WHERE pk=?";
if (sqlite3_prepare_v2(database, sql, -1, &init_statement, NULL) != SQLITE_OK) {
NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
}
// For this query, we bind the primary key to the first (and only) placeholder in the statement.
// Note that the parameters are numbered from 1, not from 0.
sqlite3_bind_int(init_statement, 1, primaryKey);
if (sqlite3_step(init_statement) == SQLITE_ROW) {
self.text = [NSString stringWithUTF8String:(char *)sqlite3_column_text(init_statement, 0)];
} else {
self.text = @"Nothing";
}
// Reset the statement for future reuse.
sqlite3_reset(init_statement);
}
return self;
}

@end

Thursday 23 June 2011

IPhone SDK : Using Table Views and Implements Their DataSource,Delegate

SelectFile > New Project or Apple Key + Shft + N to bring up the new project menu. Select the Applications item of the IPhone OS section from the menu on the left, and select View Based Application from the icons on the right. When prompted enter a project name, I have used TableView in the sample code.


There are four files in the Classes package;
TableViewTutorialAppDelegate.h
TableViewTutorialAppDelegate.m
TableViewTutorialViewController.h
TableViewTutorialViewController.m


Step 2: Create the Data Controller Class



In addition to the four files generated automatically we need to create a new class (and hence assosiated header and source files) to store the data to be displayed in the view. Select File > New File or press the the Command Key + N to open the New File Dialog. Create a new source file of type NSObject Subclass (I used the name DataController.m) and make sure you select the checkbox labeled Also create "DataController.h" file (This will be different based on the name you gave your source file).






In the DataController.h file add an instance of NSMutableArray to store actual data and also add helper methods to return size of array, item at a particular index and to add and delete items. Also add a property corresponding to the list to allow access. The complete file should look something like this:

1 #import
2 @interface DataController : NSObject
3 {
4 NSMutableArray *list;
5 }
6 - (unsigned)countOfList; //returns number of elements in list
7 - (id)objectInListAtIndex:(unsigned)theIndex; //returns object at given index
8 - (void)addData:(NSString*)data; //adds data to the list
9 - (void)removeDataAtIndex:(unsigned)theIndex;
10 @property (nonatomic, copy, readwrite) NSMutableArray *list;
11 @end

Step 2 b: Implement methods of Data Controller
First synthesize the getters and setters for the list proporty by adding @synthesize to after line 11 in DataController.m. Now implement the methods defined in the header file.

Method: countOfList
The simplest method is the list count, just call the count method on the list property and return the result.
1 - (unsigned)countOfList
2 {
3 return [list count];
4 }


Method: objectInListAtIndex
Next and just as simple is to return the element at a specific index of the list just call the objectInListAtIndex method of the list and return the result.

1 - (id)objectInListAtIndex:(unsigned)theIndex
2 {
3 return [list objectAtIndex:theIndex];
4 }


Method: removeDataAtIndex
Just add the call to the list structure.

1 - (void)removeDataAtIndex:(unsigned)theIndex
2 {
3 [list removeObjectAtIndex:theIndex];
4 }


Method: addData
I am currently using an NSString to store data but you probably want to create a domain object in a real program.

1 - (void)addData:(NSString*)data;
2 {
3 [list addObject:data];
4 }

Method: setList
We also override the set list method to make sure the mutable array remains mutable.

1 // Custom set accessor to ensure the new list is mutable
2 - (void)setList:(NSMutableArray *)newList
3 {
4 if (list != newList)
5 {
6 [list release];
7 list = [newList mutableCopy];
8 }
9 }



Method: Init and dealloc
Used to initilize the objects and free mermory respectively.

1 - (id)init
2 {
3 if (self = [super init])
4 {
5 //Instantiate list
6 NSMutableArray *localList = [[NSMutableArray alloc] init];
7 self.list = localList;
8 [localList release];
9
10 //Add initial Data
11 [self addData:@"Item 1"];
12 [self addData:@"Item 2"];
13 }
14 return self;
15 }


1 - (void)dealloc
2 {
3 [list release];
4 [super dealloc];
5 }



Step 3: Update Table View Controller

Currently the TableViewTutorialViewController inherits from the UIViewController class we need to change this to the UITableViewController. Moreover we need to add a data controller class to supply data to be displayed in the rows of the table view. For this we will add the @class declarative to our view controller header file and also create an instance of this data view class and add a property for that instance. In addition we also create a view to store the tableview and create a corresponding property. The code for TableViewTutorialViewController.h should look something like this:

1 #import

2 @class DataController;
3 @interface TableViewTutorialViewController : UITableViewController
4 {
5 DataController *dataController;
6 UIView * myView;
7 }
8
9 @property (nonatomic, retain) DataController *dataController;
10 @property (nonatomic, retain) UIView * myView;
11 @end


Step 4: Implement Table View Controller Class
The code for the of TableViewTutorialViewController.m file is shown below I will individually explain the purpose of individual lines or groups of lines . Broadly what we are doing is drawing the interface (See initWithStyle and loadView), connecting it to this controller object (See loadView line 24 and 25) and then implementing the callbacks and event handlers. (See all other mehods)

//Line 1 should already exist in the auto generated file but add line to because we are going to be using methods
//from the data controller class.
1 #import "TableViewTutorialViewController.h"
2 #import "DataController.h"

//Line 3 is auto generated but we add lines 4 nd 5 to create getters and setters for the corresponding attributes
3 @implementation TableViewTutorialViewController
4 @synthesize dataController;
5 @synthesize myView;


Method: initWithStyle
//Constructor Equivelent: used to initilize view controller (self) and data controler
6 - (id)initWithStyle:(UITableViewStyle)style
7 {
8 if (self = [super initWithStyle:style])
9 {
10 self.dataController = [[DataController alloc] init];
11 }
12
13 return self;
14 }


Method: loadView

//Define the interface and connect to controller object by specifying self as delegate and data source

15 -(void)loadView
16 {
17 // create and configure the view
18 CGRect cgRct = CGRectMake(0, 10, 320, 400); //define size and position of view
19 myView = [[UIView alloc] initWithFrame:cgRct]; //initilize the view
20 myView.autoresizesSubviews = YES; //allow it to tweak size of elements in view
21 self.view = myView; //set view property of controller to the newly created view
22 UITableView * tableView = [[UITableView alloc] initWithFrame:cgRct style:UITableViewStylePlain];
23 tableView.editing = YES; //This allows user of progrm to add or remove elements from list
24 tableView.dataSource = self;
25 tableView.delegate = self; //make the current object the event handler for view
26
27 [self.view addSubview:tableView];
28 }


Method: numberOfSectionsInTableView

//We have to implement this as the object is the data source for the table view
//Hard coded number of sections in table to 1 as we are only making a single list for this example
29 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
30 {
31 return 1;
32 }


Method: numberOfRowsInSection
//We have only one section and that section has a single datasource so we just return the number of elements in
//the datasource. We have the plus one because we want to add a speacial item at the top of the list which allows //us to add more items to the list. We see how that is done later

33 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
34 {
35 // Only one section so return the number of items in the list
36 return [dataController countOfList]+1;
37 }


Method: cellForRowAtIndexPath

//This is a call back invoked by the interface when drawing the table view. This method will create a cell for each
// row and add text to each cell dependeing on the string retrieved from the datasource. Note this is called for each
/index from zero to the number or rows returned by the previous method (numberOfRowsInSection). The zeroth
//row is hard coded to display the text "New Item" this is used to add new rows to the table.

38 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
39 {
//Try to get rusable cell
40 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
41 if (cell == nil)
42 {
//If not possible create a new cell
43 cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,0,0) reuseIdentifier:@"CellIdentifier"]
autorelease];
44 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
45 }

46 // Get the string to display and set the value in the cell
47 if(indexPath.row == 0)
48 {
//The first (or zeroth cell) contains a New Item string and is used to add elements to list
49 cell.text = @"New Item...";
50 }
51 else
52 {
//Retreive text from datasource, the -1 accounts for the first element being hardcoded to say new Item
53 cell.text = [dataController objectInListAtIndex:indexPath.row-1];
54 }
55 return cell;
56 }



Method: editingStyleForRowAtIndexPath

//This defines for each row its editing style, i.e. whether it shows a remove sign (Red circle with subtract sign) or
//and add sign (Green circle with addition sign). I have hard coded the first row (the one that says "New Item") to display the add sign and all others to display the subtract sign.

57 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:
(NSIndexPath *)indexPath
58 {
59 if(indexPath.row == 0)
60 {
61 return UITableViewCellEditingStyleInsert;
62 }
63 else
64 {
65 return UITableViewCellEditingStyleDelete;
66 }
67 }



Method: commitEditingStyle
//This method is invoked when the user has finished editing one of the rows of the table. The three parameters
//respectivly proivide, the table being edited, the style of the row being edited (Add or Delete) and the row being
//edited. If the style is delete we remove the corresponding item from the data source and then delete the row from
///the view. If the style was add we add another element to the data source and relode the data into the table view.
//In reality add item will probably load a new view which allows the user to enter text but that is left to another
//tutorial for now we are hard coding the text to be added.

68 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
69 {
70 // If row is deleted, remove it from the list.
71 if (editingStyle == UITableViewCellEditingStyleDelete)
72 {
73 [dataController removeDataAtIndex:indexPath.row-1];
74 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
75 }
76 else if(editingStyle == UITableViewCellEditingStyleInsert)
77 {
78 [dataController addData:@"New Row Added"];
79 [tableView reloadData];
80 }
81 }


82 - (void)dealloc
83 {
84 [super dealloc];
85 }

86 @end

Step 5: Loading the TableView
In the TableViewTutorialAppDelegate.m file's applicationDidFinishLaunching method initialize the tableview controller and add it as a sub view. The code should look something like this.

1 - (void)applicationDidFinishLaunching:(UIApplication *)application
2 {
3 viewController = [[TableViewTutorialViewController alloc] initWithStyle:UITableViewStylePlain];
5 [window addSubview:viewController.view];
6 [window makeKeyAndVisible];
7 }

Step 6: Try it out

Wednesday 15 June 2011

How To Manage NSMutableDictionary And NSMutableArray

//create the dictionary
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];

//add keyed data
[dictionary setObject:@"Object One" forKey:@"1"];
[dictionary setObject:@"Object Two" forKey:@"2"];
[dictionary setObject:@"Object Three" forKey:@"3"];

//write out Object Three to the log
NSLog(@"%@", [dictionary objectForKey:@"3"]);

//release the dictionary
[dictionary release];

//create the mutablearray
NSMutableArray *arrayOfObjects = [[NSMutableArray alloc] init];
[arrayOfObjects addObject:@"Object One"];
[arrayOfObjects addObject:@"Object Two"];
[arrayOfObjects addObject:@"Object Three"];
[arrayOfObjects addObject:@"Object Four"];
[arrayOfObjects addObject:@"Object Five"];

//write out Object to the log
NSLog(@"Object = %@", [arrayOfObjects objectAtIndex:object no]);