19 Mayıs 2015 Salı

Create a new Xcode Project in Swift Language

1. Launch Xcode



You can access Xcode from /Developer/Applications. When Xcode is launched, you see the Welcome screen. In the right column, most recent projects which you open are listed.



If you want to use Xcode without the Welcome screen , deselect the "Show This Window When Xcode Launches" check box.






2. Create a new Xcode project 


There are two ways to create a new project

a. Select  a new Xcode project

b. Select File->New-> Project
  

3.  Select an application type



There are a few templates in the New Project window such as Master-Detail Application, Page Based application, game etc.  According to your need , you can choice one of them.



4. Enter a project name and select language and device type.


   In the this window, project options are listed. You can choose iPhone, iPad or universal as device type and Swift or Objective-C as language. 




5. Choose Project location in your computer 



19 Şubat 2015 Perşembe

Mobile Developer Questions and Answers

  • What’s the difference between directly calling a object’s method and perform 
Selector on a method? 

"performSelector": method allows you to send messages that aren’t determined until runtime. We can use main thread or another thread with"performSelector" or we can add delay time.

                    "directly calling a object’s method" : is a call to a known method on a known object. 

  • Do you use Automated Reference Counting? What are some of its 
(dis)advantages? 

I used only with third party library. ARC helps us to save time and skip the writing of deallocs and other memory management calls. When we develop in past projects without ARC, we must implement ARC for all of the class files so we lost a lot of time for past projects.

  • What does the @dynamic keyword do?  When might you use it? 

@dynamic: the keyword provides us to use accessor methods dynamically at runtime. It can be use for the Objective-C runtime functions

  • When might you use a CFArray/Dictionary instead of a NSArray/Dictionary? 
  
When we want to store non-reference-counted objects, we can create a CFArray or a CFDictionary.  


  • What is toll-free bridging and when is it useful? 

The some data structures are interchangeable. When we use the type on one side of the bridge, we can use the other. For example, we can create a CFString and then send NSString messages to it.

  • What’s a Objective C category and when might you use one? 

A category provides us to add methods to an existing class. We can extend the functionality of existing classes without subclassing

What usually (should) happens in a view controller when your app gets a low memory   warning? 

When the view controller gets a low-memory warning, it should be prepared to reduce its memory usage if it is not visible onscreen

  • What are blocks? What does the block keyword do? 

Blocks allow us to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects, which means they can be added to collections like NSArray orNSDictionary. They also have the ability to capture values from the enclosing scope, making them similar to closures or lambdas in other programming languages.



10 Şubat 2015 Salı

Swift Basics - Array and Dictionary


Simple Values

let -> make a constant
var -> make a variable 

In order to assign a value, we may not write type explicitly

Example:

    let constantValue = 20
        let constantValue2 : Int = 20

 Error:  Cannot assign to 'let' value 'constantValue'
constantValue = 30.5
 Error:  Cannot assign to 'let' value 'constantValue2’
        constantValue2 = 30

Change let to var
  var Value = 20
        var Value2 : Int = 20
      
 Error:  Type 'Int' does not conform to protocol 'FloatLiteralConvertible'
        Value = 30.5
  It’s OK
        Value2 = 30


Convert to “Int” type to “String” type

let stringValue = "The height of frame is "
        let height = 100
        let ​heightLabel​ = stringValue + String(height)
        
        
        println(​heightLabel​)

Convert to any type to String type basically.


  let height = 100
        let stringValue = "The height of frame is  \(height)"
        let ​heightLabel​ = stringValue
        
        
        println(​heightLabel​)


Change an item of array

        var arrayList = ["cat","dog","bird"]
        arrayList[1] = "penguen"
        
        for var i=0; i<arrayList.count; i++
        {println(arrayList[i])
        }

Add an item of array by using append method

        arrayList.append("bear")
 

Add an array of one or more compatible items
        arrayList += ["cow","butterfly"]

To insert an item into the array at a specified index:
        arrayList.insert("bee", atIndex: 4)

To remove an item from array
  arrayList.removeAtIndex(5)









3 Şubat 2015 Salı

Dynamic TableView Height using AutoLayout in Swift









iPhone has different sizes. In order to a tableview's height is suitable for any iPhone size, we can use autolayout.

An examle for Dynamic TableView Height using AutoLayout in mobile development

1. Add a tableview in view and implement UITableView Delegate and Data Source








class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
...




2. Define tableview width

For Tableview width = iPhone width , we should define "equals width"



3. Add top and bottom space constraints

For Statusbar space, we should define "20"  top space
For horizontal space and vertical bottom space,  we should define "0". Since we define bottom space  as  "0" , tableview height has dynamic height.


4.  Control the storyboard preview for different iPhone sizes




5. Add an outlet object for TableView



5. Register tableview class


Add register class method in viewDidLoad

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")


6. Add tableview methods for testing

  func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
        
        cell.backgroundColor = UIColor.clearColor()
        cell.textLabel.text = "item \(indexPath.row)"
        
        
        
        return cell
    }
    
    func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
        println("Tablecell #\(indexPath.row)!")
    }


7. Result for iPhone 6




You can download from https://github.com/ozlemakalin/DynamicTableViewHeightSwift


   


26 Ocak 2015 Pazartesi

Basic AutoLayout Example in iOS -2








Let's make a basic autolayout example by using two label , an imageview and a scrollview.


Firstly add an imageview the upper left corner.

height = 80
width = 80

We should add constraints for the imageview and define width and height sizes.




Secondly add a label right of imageview.
In order to add top space, we choose Top space to Top Layout Guide
In order to save space between imageview and label, we choose left space.


Add a scrollview below imageview and label.
We should add constraints for the scrollview and define width and height sizes.
In order to change size of scrollview according to iPhone sizes, we choose "Equal Widths" for its width.

Also ,we must design its height size according to iPhone sizes. Therefore we set 0 bottom space. And we set 0 left space.


Finally, let's add a label center of scrollview. We must choose "Center Horizontally in Container" and "Center Vertically in Container

Update Frames all views :)

Let's control all views for different iPhone sizes.


Result






          

24 Ocak 2015 Cumartesi

Basic AutoLayout Example in iOS - 1

Let's make a basic autolayout example.

Firstly add a blue view the upper left corner.

Add the constraints for the blue view

height = 100
width = 100


Update Frames for blue view






Seconly, add a purple view the center

Add the constraints for the purple view

height = 200
width = 200


In order to show a view on the center, we must add alignment constraints:
1. Horizontal center in Container
2. Vertical center in Container


Update Frames for purple view


Finally add a orange view the upper left corner.

Add the constraints for the orange view

height = 100
width = 100

Update Frames for orange view

Let's control storyboard preview for iPhone sizes.




Result for iPhone 6








You can download from  https://github.com/ozlemakalin/BasicAutoLayoutExample





15 Ocak 2015 Perşembe

Custom Button in Swift



Custom Buttons 

Create a swift file for custom button










Create a custom button class



class CustomButton: UIButton {
     required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.borderColor = UIColor.yellowColor().CGColor
        self.layer.borderWidth = 5
        self.backgroundColor = UIColor.redColor()
        
}


}


Define custom button class in Storyboard






For custom button with imageview and label

Create view and add imageview and label in view
Add button on image view and label
Define constraints







 Result



You can download from https://github.com/ozlemakalin/custombuttonswift