Sunday, May 25, 2014

iOS Tutorial - Part 10 - Review

Review


Video Description 

Warnings

Xcode shows warning with yellow rectangle at the beginning of the line of the code that has problem. You can see the description of the warning by clicking on the rectangle. If the description of the warning is more than a line, you can hover the warning to show the full description of the warning.
In order to force the Xcode compiler to have a warning, we can use #warning message like bellow:
#warning Description of the warning

Errors

Xcode shows compile errors with red signs. The build will fail if application contains any error.

(nonatomic, strong)

nonatomic: Not thread safe. You can read the full description of this attribute here
strong: Strong pointer to an object. It means keep the memory for the thing that this property points to, in the heap,as long as I or anyone else has strong pointer to it. Full description could be found here
weak: Weak pointer to an object.

#import

In order to include other classes or other frameworks we use #import. We import only .h files. The format for importing classes is as follow:
#import "ViewController.h"
The format for importing a framework is as follow:
#import ""

Why self?

If you define a property and you want to call that property, you need to use self at the beginning of the name of the property. For local or instance variables we don't need to use self. If you are familiar with Java development, self is more like this in Java.

Dot notations and square bracket notation

You may have noticed dot notations in objective-c. They both do the same thing, accessing the methods.Use dot notation only for setters and getters, but for calling a method, use square bracket notation.
self.myTextField.text.length
The equivalent for the above line is as follow:
[[[self myTextField] text] length];
We can use the combination of dot notation and bracket notation like bellow:
[self.myTextField.text length];

MVC

Model-View-Controller
Full explanation of MVC design pattern in objective-c can be found here (Minute 14:18 until 26:00).

Wednesday, April 30, 2014

iOS Tutorial - Part 9 - NSNumber, id Type

NSNumber, id Type

Video Description 
 

NSNumber

It defines a set of methods specifically for setting and accessing the value.
@property (nonatomic, strong) NSNumber *myNumber;
numberWithInt
A wrapper around int to convert it to an object.
int myInt = 2;
self.myNumber = [NSNumber numberWithInt:myInt]; 
NSNumber has other useful methods for the other primitive types. Methods like: numberWithDouble, numberWithFloat, numberWithBool, numberWithChar, ...
Primitive types
Here are the important primitive types in Objective-C: int, BOOL, float, char, double, short, long.
stringValue
You can convert int type into string by using NSNumber method.
NSString *myStrig = [self.myNumber stringValue];
We have some similar methods like floatValue, intValue, boolValue, shortValue, doubleValue that can convert primitives into each other.

Type id

When we don't know the type of an object we use id as a type. It is useful when we want to assign a variable to a type but we are not sure what kind of object we receive (We will have more examples in the future).
@property (nonatomic, strong) id UnknownP;

Sunday, April 13, 2014

iOS Tutorial - Part 8 - Local Variable, NSDictionary, NSMutableDictionary

Local Variables, NSDictionary, NSMutableDictionary


Video Description 

Local Variables

You can define a local variable and in the same line you can also initialize it by allocating memory for it like bellow:
NSArray *myArray = [[NSArray alloc] init];
NSString *myLocalString = [[NSString alloc] init];

Useful methods of NSDictionary

The NSDictionary class declares the programmatic interface to objects that manage immutable associations of keys and values. We can declare and initialize a dictionary with keys and values with this method dictionaryWithObjectsAndKeys
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"value0", @"key0",
                            @"value1", @"key1",
                            @"value2", @"key2",
                            nil];

count
Returns the number of keys or values (number of keys and values are equal)
NSLog(@" %d", myDict.count);

objectForKey:key
Returns the value for the specified key
NSLog(@" %@", [myDict valueForKey:@"value0"]);
allKeys
Returns an array containing all keys in the dictionary
NSLog(@" %@", [myDict allkeys]);
allValues
Returns an array containing all values in the dictionary
NSLog(@" %@", [myDict allValues]);

Useful methods of NSMutableDictionary

setObject:anObject forKey:key
Adds an object with specified key to the dictionary
[myMuteDict setObject:@"Apple" forKey:@"ios"];
[myMuteDict setObject:@"Google" forKey:@"android"];

removeObjectForKey:key
Removes an object that has the specified key
[myMuteDict removeObjectForKey:@"android"];

removeAllObjects
Removes all of the objects from the dictionary
[myMuteDict removeAllObjects];

Sunday, February 16, 2014

iOS Tutorial - Part 7 - NSArray, NSMutableArray

Download finished project from here: Google Drive Download Link

NSArray, NSMutableArray


Video Description
NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. Here is the documentation of this class.

Initialize NSArray

We already know how to define NSArray property and also how to synthesize it. But let's log the value of the array and see what would we get.
Define the property:
@property (nonatomic, strong) NSArray *myArray;
Synthesize it:
@synthesize myMutableArray = _myMutableArray;
Log it like bellow:
NSLog(@" %@", self.myArray);
We expect that the result shows an empty array but The result will be null. Why? Because we have not initialized the array. Before initializing the array, there is no memory allocated for this array. By initializing the array we allocate memory for it then we can send message to that array and get a log for it. The best place to initialize the array property is in it's getter.

Equivalent for Synthesize

As I mentioned in tutorial part 4 When we synthesize properties, in fact we are creating setter and getter for that property. If we want to manually create the setter and getter for the property, we have something like bellow:
Setter
- (void)setMyArray:(NSArray *)myArray
{
    _myArray = myArray;
}

Getter
- (NSArray *)myArray
{
    return _myArray;
}

We need to modify the getter to initialize our array whenever we need the array. We allocate memory like bellow:
- (NSArray *)myArray
{
    if (_myArray == nil) _myArray = [[NSArray alloc] init];
    return _myArray;
}
The if statement above means that, if myArray is null, allocate memory for it and then initialize it for me. Now if we log the array, it shows an empty array. Since we don't change anything in setter method we can delete it.

Useful methods of NSArray

arrayWithObjects
You can define array items by using this method like bellow:
    
self.myArray = [NSArray arrayWithObjects:@"object zero",@"object one" , nil];

count
Returns number of items that array holds
NSLog(@" %d", self.myArray.count);
objectAtIndex:
Returns the value of the item that is located in the specified index
NSLog(@" %@", [self.myArray objectAtIndex:1]);
lastObject
Returns the value of the last object of the array
    NSLog(@" %@", [self.myArray lastObject]);

Useful methods of NSMutableArray

NSMutableArray has all of NSArray methods, plus the following methods:
addObject
Adds an object to the end of the array
[self.myMutableArray addObject:@"object 1"];
insertObject: atIndex:
Inserts an specified item to the array at specified index
[self.myMutableArray insertObject:@"Hi" atIndex:1];
removeObjectAtIndex:
Removes the object at specified index
[self.myMutableArray removeObjectAtIndex:0];
removeLastObject
Removes the last object of the array
[self.myMutableArray removeLastObject];

All of this tutorial code

//
//  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) NSArray *myArray;
@property (nonatomic, strong) NSMutableArray *myMutableArray;

@end

@implementation ViewController
@synthesize myArray = _myArray;
@synthesize myMutableArray = _myMutableArray;


//Setter for myArray
- (void)setMyArray:(NSArray *)myArray
{
    _myArray = myArray;
}

//Getter for myArray
- (NSArray *)myArray
{
    //Check if the array is null, allocate memory for it and then initilize it
    if (_myArray == nil) _myArray = [[NSArray alloc] init];
    return _myArray;
}

//Getter for myMutableArray
- (NSArray *)myMutableArray
{
    if (_myMutableArray == nil) _myMutableArray = [[NSMutableArray alloc] init];
    return _myMutableArray;
}

//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];
    
    //NSArray
    
    //Log static values
    NSLog(@" %@", self.myArray);
    //Result: ()
    self.myArray = [NSArray arrayWithObjects:@"object zero",@"object one" , nil];
    NSLog(@" %@", self.myArray);
    /* Result:
    (
     "object zero",
     "object one"
     )
     */
    
    
    // %d ==> for integer type
    // %g ==> for float type
    NSLog(@" %d", self.myArray.count);
    //Result: 2
    
    NSLog(@" %@", [self.myArray objectAtIndex:1]);
    //Result: object one
    
    NSLog(@" %@", [self.myArray lastObject]);
    //Result: object two

    //NSMUtableArray
    [self.myMutableArray addObject:@"object 0"];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     "object 0"
     )
     */
    
    [self.myMutableArray addObject:@"object 1"];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     "object 0",
     "object 1"
     )
     */
    
    [self.myMutableArray insertObject:@"Hi" atIndex:1];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     "object 0",
     Hi,
     "object 1"
     )
    */
    
    [self.myMutableArray removeObjectAtIndex:0];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     Hi,
     "object 1"
     )
     */
    
    
    [self.myMutableArray removeLastObject];
    NSLog(@" %@", self.myMutableArray);
    /* Result:
     (
     Hi
     )
     */

}

@end
Download finished project from here: Google Drive Download Link

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

Saturday, February 8, 2014

iOS Tutorial - Part 5 - Foundation Frameworks

Foundation Frameworks (Property and variable types)


Video Description

NSObject

NSObject is the root class of most Objective-C class hierarchies. Through NSObject, objects inherit a basic interface to the runtime system and the ability to behave as Objective-C objects (Don't worry if you don't understand the definition, we will talk about it later).

NSString & NSMutableString


In order to define a String type variable or property in Objective-C language we use NSString like bellow:
@property (nonatomic, strong) NSString *myString;

If you want to modify the existing string, instead of NSString type you should use NSMutableString, which is mutable version of the NSString
@property (nonatomic, strong) NSMutableString *myMutableString;

NSArray & NSMutableArray


Ordered collection of objects. In order to define an Array type variable or property in Objective-C language we use NSArray like bellow:
@property (nonatomic, strong) NSArray *myArray;
If we want to add or remove object to the array or any other modification we have to use NSMutableArray type like bellow:
@property (nonatomic, strong) NSMutableArray *myMutableArray;

NSDictionary & NSMutableDictionary


Look up objects using a key to get a value. In order to define a dictionary with keys and values in Objective-C language we use NSDictionary like this:
@property (nonatomic, strong) NSDictionary *myDictionary;
If we want to have a modifiable version of NSDictionary we should use NSMutableDictionary type like bellow:
@property (nonatomic, strong) NSMutableDictionary *myMutableDictionary;

NSNumber

Object wrapper around primitive types like int, float, double, BOOL, etc. It is useful when you want to put these primitive types in an NSArray or NSDictionary. We can define it like bellow:
@property (nonatomic, strong) NSNumber *myNumber;

Sunday, January 19, 2014

iOS Tutorial - Part 4 - Instance variables, methods, property

Objective-C instance variables, methods and properties


Video Description
There are two popular types of classes in Objective-C language, files end with .h and files end with .m

.h files

If you want to define public variables or public methods, you need to declare them in .h file. Simple .h file would look like this.
#import <UIKit/UIKit.h>

@interface ClassName : UIViewController

@end
 UIViewContoller is the super class of our class.

.m files

If you want to define private variables or private methods, you need to declare them in .m file. Simple .m file would look like this. 
#import "ClassName.h"

@interface ClassName ()
@end

@implementation ClassName
@end 

Instance variables

Based on the type of variable, you can easily declare them in .h or .m files.
Example of declaring variables in .h file
#import <UIKit/UIKit.h>

@interface ClassName : UIViewController {
    int variableName;
}

@end
Example of declaring variables in .m file
#import "ClassName.h"

@interface ClassName (){
    int variableName;
}
@end

@implementation ClassName

@end 

Instance Methods

Example of declaring Instance methods in .h file
#import <UIKit/UIKit.h>

@interface ClassName : UIViewController {
   int variableName;
} 
-(void) myInstanceMethodName;
 @end
Example of declaring Instance methods in .m file

#import "ClassName.h"

@interface ClassName (){
    int variableName;
}
@end

@implementation ClassName
 
-(void) myInstanceMethodName
{
   //Method implementation
}
 @end

Properties

In terms of memory management it's better to use properties instead of instant variables. It's more efficient and easier to use. It also creates setter and getter by writing one line of code. You declare properties like below:
@property (nonatomic, strong) NSString *propertyName;
NSString means the property, is type of string. You need to add * before variable name. At this stage don't worry about the (nonatomic, strong), just happily use it for your properties. Later on I'll explain what are these words good for.
In order to make setter and getter for this property you need to synthesize (implement) it. You synthesize it by the following line of code:
@synthesize propertyName = _propertyName;

Example of property and synthesize in .m file
#import "ClassName.h"

@interface ClassName ()
@property (nonatomic, strong) NSString *propertyName;

@end

@implementation ClassName

@synthesize propertyName = _propertyName;

@end