- iPhone SDK: Objective-C Condition
- iPhone SDK Components & Requirements
- Overview of iOS Platform
- Objective-C Tutorial with xCode
- iPhone SDK: Objective-C Creating Custom Class
- iPhone SDK: Using Existing Objective-C Classes
- iPhone SDK: Your First iPhone Application
- iPhone SDK: MVC in iPhone Application Development
- iOS: Working with Actions & Outlets
The Foundation framework contains lots of predefined objective-c classes that we can use like NSString, NSDate etc. Let’s create a program that uses built-in objective-c classes.
Using Existing Objective-C Classes
Step 1. Create new Project in Xcode with command line tool template and write code in main method.
1 2 3 4 5 6 7 8 9 10 11 |
int main (int argc, const char * argv[]) { @autoreleasepool { NSString *name = @"Ronak Prajapati"; NSString *uname = [name uppercaseString]; } return 0; } |
First we created NSString class’s object called name and set String value to “Ronak Prajapati”. Then we called name object’s instance method uppercaseString, which return string in upper case letters and store in new object called uname.
Syntax for calling instance method or say message:
[Object method_name];
You can see the whole documentation of any class and for that you don’t have to search it on google or apple documentation website. It’s very easy. Just alt+click on class name in code.
Clicking on the book icon from the right top corner of the popup, bring you to the full documentation of that particular class. This contains all methods that are available for us in NSString class. Scroll down till you find the changing case section. Where you can see the uppercaseString Method signature. Which looks like this:
Step 2. Now lets output the new uname object using NSLog.
1 2 3 4 |
NSString *name = @"Ronak Prajapati"; NSString *uname = [name uppercaseString]; NSLog(@"Your Name is %@",uname); |
This will output: Your Name is RONAK PRAJAPATI
Step 3. Now create another object of say NSDate type
1 2 3 4 5 6 7 |
NSString *name = @"Ronak Prajapati"; NSString *uname = [name uppercaseString]; NSLog(@"Your Name is %@",uname); NSDate *d1 = [NSDate date]; NSLog(@"Todays date is %@",d1); |
NSDate has a class method called date, which returns current system date. We store that returned date in object d1 and then output using NSLog.
Syntax for calling class method or say message:
[classname method_name];
Let’s quickly hit run and see how it outputs.
Note: There are lots of built-in objective-c classes in foundation framework, which we can use in any Objective-C program. Full documentation is available for us and it’s easy to access by alt+clicking on class name.  You don’t have to learn every class and memorize it. It’s very hard, it takes lots of time and it’s not convenient way to developing iPhone application. Just make sure that whenever you need help regarding any class it’s there for you.