- Declare a string variable and release it from memory manually
NSString *str1 = [[NSString alloc] initWithString:@”Hello World …”];
NSLog(@”%@”,str1);
[str1 release];
- Declare a string variable and automatically release it from memory
NSString *str1 = [NSString stringWithString:@"Hello World ..."];
NSLog(@”%@”,str1);
OR
NSString *str1 = @”Hello World …”;
NSLog(@”%@”,str1);
- Append string variable with new string
NSString *str1 = @”Hello World …”;
str1 = [str1 stringByAppendingString:@", Goodbye world"];
NSLog(@”%@”,str1);
- Grab a substring of a given string from start till character index
NSString *str1=@”Hollo World”;
str1=[str1 substringToIndex:3];
NSLog(@”%@”, str1);
Output:
2012-05-03 20:16:42.245 Project_7[74969:903] Hol
- Grab a substring of a given string from character index till end of string
NSString *str1=@”Hollo World”;
str1=[str1 substringFromIndex:3];
NSLog(@”%@”, str1);
Output:
2012-05-03 20:16:42.245 Project_7[74969:903] lo World
- Use Dynamic array with memory allocation
NSMutableArray *DynArray = [[NSMutableArray alloc] initWithObjects:@”Hello World”, nil];
[DynArray addObject:@"Goodbye World"];
NSLog(@”%@”,DynArray);
[DynArray release];
Output:
2012-05-03 19:57:21.555 Project_7[74850:903] (
“Hello World”,
“Goodby World”
)
NSMutableArray *DynArray = [[NSMutableArray alloc] initWithObjects:@”Hello World”,@”Goodbye World”, nil];
[DynArray exchangeObjectAtIndex:0 withObjectAtIndex:1];
NSLog(@”%@”,DynArray);
[DynArray release];
Output:
2012-05-03 20:01:58.293 Project_7[74896:903] (
“Goodby World”,
“Hello World”
)
- Remove object index from array
NSMutableArray *DynArray = [[NSMutableArray alloc] initWithObjects:@”Hello World”,@”Goodbye World”, nil];
[DynArray removeObject:@"Goodby World"];
NSLog(@”%@”,DynArray);
[DynArray release];
Output:
2012-05-03 20:05:52.639 Project_7[74933:903] (
“Hello World”
)
- Use NSDictionary for store one table name
NSDictionary *DicObj = [NSDictionary dictionaryWithObjectsAndKeys:
@"Mohammed", @"FirstName",
@"Al-Atari", @"LastName",
@"23", @"Age", nil];
NSLog(@”%@” , DicObj);
NSLog(@”%@”, [DicObj objectForKey:@"FirstName"]);
Output:
2012-05-03 20:36:41.401 Project_7[75304:903] {
Age = 23;
FirstName = Mohammed;
LastName = “Al-Atari”;
}
2012-05-03 20:37:40.666 Project_7[75332:903] Mohammed
- Use NSMutableDictionary for store one table name with initializing the object
NSMutableDictionary *DicObj = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"Mohammed", @"FirstName",
@"Al-Atari", @"LastName",
@"23", @"Age", nil];
NSLog(@”%@” , DicObj);
NSLog(@”%@”, [DicObj objectForKey:@"FirstName"]);
Output:
2012-05-03 20:36:41.401 Project_7[75304:903] {
Age = 23;
FirstName = Mohammed;
LastName = “Al-Atari”;
}
2012-05-03 20:37:40.666 Project_7[75332:903] Mohammed
- Convert string to integer
int value = [textbox1.text intvalue];
- Convert integer or decimal to string to show in text field
CtrlLbl_Equal.text = [NSString stringWithFormat:@"%d", 1234];
- Use NSMutableDictionary for store one table name without initializing the object
NSMutableDictionary *DicObj = [NSMutableDictionary dictionary];
[DicObj setObject:@"Mohammed" forKey:@"FirstName"];
[DicObj setObject:@"Al-Atari" forKey:@"LastName"];
NSLog(@”%@” , DicObj);
Output:
2012-05-03 20:36:41.401 Project_7[75304:903] {
Age = 23;
FirstName = Mohammed;
LastName = “Al-Atari”;
}
int intrnd;
intend = arc4random%10 // will generate a random # from 0 – 9
- Use UiAlertView for showing a message box
UIAlertView *Alert = [[UIAlertView alloc]
initWithTitle:@”Colors”
message:@”Please choose a color”
delegate:nil
cancelButtonTitle:@”Cancel”
otherButtonTitles:@”Red”, @”Blue”, @”Green”, nil];
[Alert show];
[Alert release];
- Use UiAlertView for showing a message box and get the selected value
implement UIAlertViewDelegate on ViewController interface
@interface ViewController : UIViewController<UIAlertViewDelegate>
- (IBAction)BtnChooseColor:(id)sender {
UIAlertView *Alert = [[UIAlertView alloc]
initWithTitle:@”Colors”
message:@”Please choose a color”
delegate:self
cancelButtonTitle:@”Cancel”
otherButtonTitles:@”Red”, @”Blue”, @”Green”, nil];
[Alert show];
[Alert release];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//NSLog(@”%i”, buttonIndex);
switch (buttonIndex) {
case 1:
self.view.backgroundColor = [UIColor redColor];
break;
case 2:
self.view.backgroundColor = [UIColor blueColor];
break;
case 3:
self.view.backgroundColor = [UIColor greenColor];
break;
default:
break;
}
}
- Use UIActionSheet for showing a message box
UIActionSheet *sheet = [[UIActionSheet alloc]
initWithTitle:@”Colors”
delegate:self
cancelButtonTitle:@”Cancel”
destructiveButtonTitle:@”Delete”
otherButtonTitles:@”Red”, @”Blue”, @”Green”, nil];
[sheet showInView:self.view];
[sheet release];