#import<
#import<
@interface Myclass : NSObject
{
int num1, num2; //global variables of integer type
}
@property (readwrite,assign) int num1, num2; //set the property for those integer variable
//you use assign in a case where you cannot retain the value like BOOL or NSRect, else use retain
//If you use retain then you become the owner of that particular property and you must release that property once its use is over.
-(void)add; //add function
@end
@implementation Myclass
@synthesize num1, num2; //synthesize will automatically set getter and setter for your property
-(void)add
{
int result= num1+num2;
NSLog(@"Result of addition is %d",result);
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Myclass *obj_Myclass = [[Myclass alloc]init];
[pool addObject:obj_Myclass]; //adding object to the pool
obj_Myclass.num1 = 10; //invoking setter method
obj_Myclass.num2 = 20; //invoking setter method
[obj_Myclass add]; //calling the add method
[pool drain];
return 0;
}
@property (readonly,assign) int num1, num2;
You can change the value directly by referencing it directly, refer the code below
-(void)add
{
self->num1 = 10; //legal
self->num2 =20; //legal
int result= num1+num2;
NSLog(@"Result of addition is %d",result);
}
NSString.h
@"Programming is fun"
//declare a class
@interface Myclass : NSObject
{
}
-(void)copystring; //copy one string to another
-(void)copy_string_at_end; //append string
-(void)Equality_Check; //check whether string is equal or not
-(void)great_check; //check which string is greater
-(void)case_check; //convert from upper case to lower case and vice versa
@end
//implement the class
@implementation Myclass
-(void)copystring
{
NSString *str1 =@"This is string R1"; //str1 is string object
NSString *str2 = @"This is string R2"; //str2 is string object
//copy str1 in str2 and display result
str2 = [NSString stringWithString:str1];
NSLog(@"%@",str2); //print str2 on console
}
-(void)copy_string_at_end
{
NSString *str1 =@"This is string R1"; //str1 is string object
NSString *str2 = @"This is string R2"; //str2 is string object
//copy str2 at the end of str1
str2 = [str1 stringByAppendingString:str2];
NSLog(@"%@",str2); //print str2 on console
}
-(void)Equality_Check
{
NSString *str1 = @"Radix";
NSString *str2 = @"RadiX";
//checking whether strings are eaual or not
if([str1 isEqualToString:str2])
{
NSLog(@"str1 = str2");
}
else
{
NSLog(@"str1 != str2");
}
}
-(void)great_check
{
//here the strings are lexically compared means their ASCII values are compared
NSComparisonResult compareresult;
NSString *str1 = @"Radix";
NSString *str2 = @"RadiX";
//for case sensitive compare
compareresult = [str1 compare:str2];
//if you don't want to do perform case sensitive comparision then use the following code
//compareresult = [str1 caseInsensitiveCompare:str2];
if(compareresult == NSOrderedAscending)
{
NSLog(@"str1 is less than str2");
}
else if(compareresult == NSOrderedSame)
{
NSLog(@"str1 == str2");
}
else
{
NSLog(@"str1 > str2");
}
}
-(void)case_check
{
NSString *str1= @"this string will be in uppercase";
NSString *str2 = @"THIS STRING WILL BE IN LOWER CASE";
str1 = [str1 uppercaseString]; //convert lowercase to uppercase.
str2 = [str2 lowercaseString]; //convert uppercase to lowercase.
NSLog(@"%@",str1);
NSLog(@"%@",str2);
}
@end
Now into the main method create the object of your class and then call the methods
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
Myclass *obj = [[Myclass alloc]init];
[pool addObject:obj]; //adding object to pool
[obj copystring];
[obj copy_string_at_end];
[obj Equality_Check];
[obj great_check];
[obj case_check];
[pool drain];
return 0;
}
NSLog(@"The length of the string is %d",[str1 length]);
The above code will give you the length of the string str1.
Now let's have a neat workout with substrings section
inorder to begin i have just add a function named :
-(void)Substring_function;
-(void)Substring_function
{
NSString *str1= @"This is string R1";
//The below line of code print the string upto the specified index number starting from the leading character.
NSLog(@"String at index 3 of str1 is : %@",[str1 substringToIndex:3]);
//if you want to print the substring from the specified index then use the below function
NSLog(@"Substring from specified index is : %@",[str1 substringFromIndex:5]);
//now lets say you have a long string and you want string only from a selected index then you can use the below function.
NSLog(@"Substring from a specified index to a specified index is : %@",[[str1 substringFromIndex:3]substringToIndex:6]);
//now lets say you want to locate a particular string inside your string then in that case you can use the below code
NSRange Myrange;
//NSRange is a structure which is used to describe a range of series such as charcters in a string and objects in an array
Myrange = [str1 rangeOfString:@"R1"];
NSLog(@"The string is found at location : %d and is of length : %d",Myrange.location,Myrange.length);
//but what if the string is not found then what to do
Myrange = [str1 rangeOfString:@"R2"];
if(Myrange.location == NSNotFound)
{
NSLog(@"No such string found");
}
else
{
NSLog(@"The string is found at location : %d and is of length : %d",Myrange.location,Myrange.length);
}
}
The output of the following function will look something like this in the image given below:
@interface Myclass : NSObject
{
NSArray *arr; //global NSArray object
}
-(void)NSArray_Demo;
@end
Now as my declaration part is completed no i will be implementing Myclass using the keyword implementation
@implementation Myclass
-(void)NSArray_Demo
{
arr = [[NSArray alloc]initWithObjects:@"Ravi",@"Riki",@"Faizal",nil];
// here nil indicates the end of array elements
for(int i =0;i<[arr count];i++)
{
NSLog(@"%@",[arr objectAtIndex:i]);
}
[arr release]; //release arr.
@end
Now the final touch
Guessed it right the main method
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Myclass *obj = [[Myclass alloc]init];
[obj NSArray_Demo];
[pool drain];
return 0;
}
Run the application by pressing Run from the meny bar and select console and then Build and Go.
The entire code looks like this
@interface MyMutablearray : NSObject
{
NSMutableArray *Marr;
}
-(void)getdata;
-(void)replacedata;
-(void)displaydata;
-(void)deletedata;
@end
Now we have finished with the declaration part now lets see the implementation part
@implementation MyMutablearray
-(void)getdata
{
Marr = [[NSMutableArray alloc]initWithCapacity:3]; // initialize the array with capacity
//You can also initialize the mutable array just like you did in the case of NSArray in the above example
char chr[10]; //declare a character array
for(int i =0;i<3;i++)
{
scanf("%s",chr);//accept the input from the user
[Marr addObject:[NSString stringWithCString:chr]]; //typecast the c array and add it to the mutablearray
}
}
//The data present at index 0 will be replaced by the new object in this function
-(void)replacedata
{
[Marr replaceObjectAtIndex:0 withObject:@"Ravi"];
}
// This function will display the data present from Mutable array and will show it on the console.
-(void)displaydata
{
for(int i =0;i<[Marr count];i++)
{
NSLog(@"%@",[Marr objectAtIndex:i]);
}
}
//delete a particular index
-(void)deletedata
{
[Marr removeObjectAtIndex:0];
}
@end
Now lets make the object of our class and execute the program so hope into your main method
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
MyMutablearray *obj = [[MyMutablearray alloc]init];
[obj getdata];
[obj displaydata];
[obj deletedata];
NSLog(@"Calling displaydata function after deletedata");
[obj displaydata];
[pool drain];
return 0;
}
after execution you will get the result as shown in the image.
+ (void)staticmethodName; //declaration of static method
|
-(void)nonstaticmethodName; //declaration of non static method
|
[
yourObjectName methodName ]; // calling object method
|
[
yourClassName methodName ]; // calling static method
|
-
(void)printSomething:(NSString*)uservalue; //function with 1 para
|
-
(void)printUserName:(NSString*)username andLastName:(NSString*)lastname; //function with 2 para
|
- (void)printUserName:(NSString*)username
andLastName:(NSString*)lastname anduserAge:(int)userage; //function with 3 parameter
|