Thursday, February 13, 2014

iOS Tutorial - Part 6 - NSLog, NSString, Documentation, Comment

NSString, NSLog


Video Description
One of the important frameworks in Objective-C is NSString, there are a lot of useful methods in this frameworks that could save you multiple lines of codes. In this tutorial I will discuss some of useful ones. If you want to see all of the methods you can read the documentation here : NSString Documentation

NSLog

In order to send message to console we use NSLog API.
NSLog(@" %@", @"Hello Console");

%@: reruns the description of the object that is passed after comma (,)
@"": returns whatever string value that is between quotation ""

How to access the value of property

We access the property inside of the instance methods by putting word "self" and the "." and then name of the property. For example for the property like this :
@property (nonatomic, strong) NSString *st1;

If we want to get the value, we put it on the right side of the equation:
  = self.st1

If we want to set the value, we put it on the left side of the equation:
 self.st1 = //Whatever value

How to put comments in Objective-C

Comments are not compiled in Objective-C. They are just for developers to put brief explanation of their code. If your comment is just one line, you can add // at the beginning of the line and start typing. But if your comment is more than one line it's better to use multiple comment line format which starts with /* and you can end it with */

Useful NSString methods

stringByAppendingString
By using this method you can concatenate two strings together. See example at the end of this tutorial to learn how to use it.
stringByAppendingFormat
By using this method you can give your string(s) a format that you want. See example at the end of this tutorial to learn how to use it.
//
//  ViewController.m
//  HelloWorld
//
//  Created by HuxTek on 1/19/14.
//  Copyright (c) 2014 HuxTek. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic, strong) NSString *st1;
@property (nonatomic, strong) NSString *st2;
@property (nonatomic, strong) NSString *st3;
@end

@implementation ViewController
@synthesize st1 = _st1;
@synthesize st2 = _st2;
@synthesize st3 = _st3;


//The following method is View life cycle (Don't worry about it now I will dedicate special session for view life cyle. I used it here because I wanted to send message to console
-(void) viewDidLoad {
    [super viewDidLoad];
    //Log static values
    NSLog(@" %@", @"Hello Console");
    //Result: Hello Console
    
    
    //Set a vale for property (We set the value of st1 to be "string one"
    self.st1 = @"string one";
    
    //If we want to log the value of the st1, we should write
    NSLog(@" %@", self.st1);
    //Result: string one
    
    
    //Get a vlue of propert
    //We want to get a value of st1 and set it for st2
    //In simple word we want to copy st1 into st2
    self.st2 = self.st1;
    
    //If we want to log the value of the st2, we should write
    NSLog(@" %@", self.st2);
    //Result: string one
    
    
    //How to comments
    //One line comment by adding // at the begging of line
    //Compiler will not compile the comments
    
    /*
        Multiple comment line
        line 2
        line 3 ...
     */
    
    
    //Append (concatinate st1 to st2 by the follwing method)
    //Open bracket [ and close bracket ] is for sending message to objects
    //We send message to st1 to append st2 and at the same time we set st3 to be the result value of this operation
    
    //Lets change the value of st2 so we can differentiate st1 from st2
    self.st2 = @"string two";
    self.st3 = [self.st1 stringByAppendingString:self.st2];
    NSLog(@" %@", self.st3);
    //Result: string onestring two
    
    
    
    //String format
    //If we want to have format like bellow
    //(string one) -> **string two**
    //We should use a method called: stringByAppendingFormat
    self.st3 = [self.st3 stringByAppendingFormat:@"(%@) -> **%@**", self.st1, self.st2];
    NSLog(@" %@", self.st3);
    //Result: (null)
    
    // Result is null because we have not initialized st3
    // We initialize it with empty string before setting it to the desired format
    self.st3 = @"";
    self.st3 = [self.st3 stringByAppendingFormat:@"(%@) -> **%@**", self.st1, self.st2];
    NSLog(@" %@", self.st3);
    //Result: (string one) -> **string two**
}


@end

No comments:

Post a Comment