Search This Blog

Thursday, November 24, 2016

iOS Swift UITableView. How to scroll to a special cell

For scrolling table view to special cell(row) we need to use method scrollToRow and index of special cell.
let index = self.findIndex()
let indexPath = NSIndexPath(row: index, section: 0)
self.tableView.scrollToRow(at: indexPath as IndexPath, at: .top, animated: true)

Wednesday, November 23, 2016

How to find nearest(closest) date to today

If we have array of dates and we need to find what date is neasrest to today date. There is special method in iOS SDK API - timeIntervalSince
public func timeIntervalSince(_ date: Date) -> TimeInterval

This method will return double value - time interval between two dates - self date object and parameter date. Also this time interval can be positive or negative. So for getting nearest date we need to get absolute positive value of time interval. For that purpose we will use method fabs
public func fabs(_: Double) -> Double

Algorithm - How to define nearest date to today
func findNearestDateToToday(datesArray: [Date]) -> Date {
        let today = Date()
        let firstDate = datesArray[0]
        var min = fabs(today.timeIntervalSince(firstDate))
        var minIndex = 0
        for i in 1..<datesArray.count {
            let currentDate = datesArray[i]
            let currentMin = fabs(today.timeIntervalSince(currentDate))
            if currentMin < min {
                min = currentMin
                minIndex = i
            }
        }
        return datesArray[minIndex]
}

Sunday, November 6, 2016

FizzBuzz task in Swift

Task:
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".


Solutions


Zero case - wrong solution (for better understanding)
In this case you never see "FizzBuzz" in output console, because all items that can be divided by 3 and 5 would be already count with print "Fizz".
// zero - wrong solution
for var i in 1..<101 {
    if i % 3 == 0 {
        print("Fizz")
    } else if i % 5 == 0 {
        print("Buzz")
    } else if i % 3 == 0 && i % 5 == 0 {
        print("FizzBuzz")
    } else {
        print(i)
    }
}


First - simple and easy
// first - for loop
for var i in 1..<101 {
    if i % 3 == 0 && i % 5 == 0 {
        print("FizzBuzz")
    } else if i % 3 == 0 {
        print("Fizz")
    } else if i % 5 == 0 {
        print("Buzz")
    } else {
        print(i)
    }
}

Second - using magic number 15 because 3 * 5 = 15
//second - foor loop and magic number 15 = 5 * 3
for var i in 1..<101 {
    if (i % 15 == 0) {
        print("FizzBuzz")
    } else if i % 3 == 0 {
        print("Fizz")
    } else if i % 5 == 0 {
        print("Buzz")
    } else {
        print(i)
    }
}


Third - using case instead of if..else statements
// third - for loop and case operator instead of switch
for var i in 1..<101 {
    switch i {
    case _ where i % 15 == 0:
        print("FizzBuzz")
    case _ where i % 3 == 0:
        print("Fizz")
    case _ where i % 5 == 0:
        print("Buzz")
    default:
        print(i)
    }
}

Fourth - make result string
//fourth - make a result string
for var i in 1..<101 {
    var str = ""
    if i % 3 == 0 {
        str = str + "Fizz"
    }
    if i % 5 == 0 {
        str = str + "Buzz"
    }
    if str == "" {
        str = String(i)
    }
    print(str)
}

Tuesday, November 1, 2016

ios Swift. How to add SegmentedControl in NavigationBar and keep title

What if we want big navigation bar - with title and segmented control under it. How to create this? There is a trick. We can create view under navigation bar, put segmented control in it, make color of navigation bar and color of container view the same and remove botton line of navigation bar.

Steps of creation:

  1.  Add View under Navigation Bar
  2.  Put Segmented Control in this view
  3.  Set Navigation Bar NOT Translucent
  4.  Set background and tint colors for navigation bar
  5.  Set background color for segmented control
  6.  Remove bottom line of Navigation Bar

We will put the code in didFinishLaunchingWithOptions method and use UIAppearance API for UISegmentedControl and for UINavigationBar.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:
        [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        // get segmented appearance object
        let segmentedAppearance = UISegmentedControl.appearance()
        
        // set background color for segmented
        segmentedAppearance.tintColor = UIColor.white
        
        // get nav bar appearance object
        let navigationAppearance = UINavigationBar.appearance()
        
        navigationAppearance.isTranslucent = false
        
        // change color of navigation bar background
        navigationAppearance.barTintColor = UIColor.red
        
        // change color of navigation bar items (buttons)
        navigationAppearance.tintColor = UIColor.white
        
        // change color of navigation bar title
        navigationAppearance.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white];
        
        // remove bottom line for navigation bar
        navigationAppearance.shadowImage = UIImage()
        navigationAppearance.setBackgroundImage(UIImage(), for: .default)
        
        return true
    }


Also do not forget to set color of container view
override func viewDidLoad() {
        super.viewDidLoad()
        
        segmentContainer.backgroundColor = UIColor.red
}

Adding container view under navigation bar

After add views and setup colors - one issue is still here. It is bottom line of Navigation bar.


Finally removing bottom line 



iOS Swift. NotificationCenter How to post and receive local notifications

How to transfer data across the application, for example when loading is finished and you need to update displayed information. For that purpose you can use Notification Center and Local Notifications. There are three thing you need:

1. Add Observer
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.updateCountLabel(_:)), 
    name: Notification.Name("UpdateCountLabelObserver"), object: nil)


2. Add function to handle notification
func updateCountLabel(_ notification: Notification) {
    guard let countStr = notification.userInfo?["count"] as? String else {
        return
    }        
    label4.text = "\(countStr)"
}

3. Post notification
let dict = ["count": String(55)]
NotificationCenter.default.post(
    name: Notification.Name("UpdateCountLabelObserver"), object: nil, userInfo: dict)




iOS Swift. How to work with DateFormatter

How to display date in specific format. For example, 01 August 1989,  or in specific locale (Russian: 1 августа 1989) or in specific timezone(MSK or PST)
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd MMM yyyy HH:mm"
label1.text = "With Format: \(dateFormatter.string(from: date))"
        
let locale = Locale(identifier: "ru")
dateFormatter.locale = locale
label2.text = "With RU Locale:  \(dateFormatter.string(from: date))"
        
let timeZone = TimeZone(abbreviation: "MSK")
dateFormatter.timeZone = timeZone
label3.text = "With MSK TimeZone: \(dateFormatter.string(from: date))"

Result


What if we get string with data in specific format and we need to display it in other format(and in specific timezone and specific locale). 

For example, 
input format: yyyy-MM-dd'T'HH:mm:ss.SSSZ
output format: dd MMMM yyyy HH:mm

Firstly, we set input format to DateFormatter and convert input string to date. After that we set nre format and convert date to new string.
 let dateStr = "2016-10-31T14:51:21.780+08:00"
 label4.text = "Before: \(dateStr)"
 let dateFormatter = DateFormatter()
 dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        
 let timeZone = TimeZone(abbreviation: "MSK")
 dateFormatter.timeZone = timeZone
        
 let locale = Locale(identifier: "ru")
 dateFormatter.locale = locale
        
 let date = dateFormatter.date(from: dateStr)
        
 dateFormatter.dateFormat = "dd MMMM yyyy HH:mm"
 let formattedDateStr = dateFormatter.string(from: date!)
        
 label5.text = "After: \(formattedDateStr)"

Conversion Result


Sunday, October 30, 2016

iOS Swift. How to hide back Button Text in UINavigationBar


By default in Navigation Controller the title of back button of current view controller is the title of previous view controller. But what if we want not show any text for back button and show only symbol "<". For that purpose we have to replace in first view controller back button for navigation item.

import UIKit

class ParentViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Remove title from back button. Now only "<" symbol will be show as back button.
        let backButton = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
        self.navigationItem.backBarButtonItem = backButton
    }

}

Standard "back button"




No text Back Button




Wednesday, October 26, 2016

iOS Swift. UiTableView how to add fixed footer to bottom

If you want to have fixed footer in bottom of view controller which will be not scroll with the table then you can not use UITableViewController. Your view controller must be implemented as UIViewController. Table view and footer view must be added as subviews to root view of view controller. Your view controller must conform to the UITableViewDataSource and UITableViewDelegate protocols.



iOS Swift. How to hide empty cells in UITableView

By default empty space in table view displayed as empty cells.


To make this empty space not displayed as empty cells (rows) we need to set table footer as empty UIView
override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.tableFooterView = UIView()
}

Result


Tuesday, October 25, 2016

XCode 8. How to disable unwanted logs

If you start using new XCode 8 you can see a lot of unwanted logs in console. So for removing this console garbage you need to do the following:

  1. Open XCode Menu > Product > Scheme > Edit scheme...
  2. You need to add Environment Variable "OS_ACTIVITY_MODE" and set its value to disable. 


Monday, October 24, 2016

iOS Swift. How to change color of status bar

First of all for changing style of Status Bar for all controllers in our application we need to add property "View controller-based status bar appearance" to Info.Plist file and set it's value to "NO".



Now for changing style of Status Bar we need to put this code in AppDelegate.Swift file in method didFinishLaunchingWithOptions
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        UIApplication.shared.isStatusBarHidden = false
        UIApplication.shared.statusBarStyle = .lightContent
        
        return true
    }
    
}

Also we can change Status Bar style in Project Setting




Default style of Status bar (Black)



Light style of Status bar (White)




iOS Swift. Change background color of Navigation Bar

In Navigation Bar we can change color for three items:
  1. Background color of Navigation Bar
  2. Color of items in Nvaigation Bar (buttons)
  3. Color of title of Navigation Bar
To change background color of Navigation Bar we will use UIApperance API
All code must be placed in AppDelegate.swift file in method didFinishLaunchingWithOptions
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: 
        [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        // get appearance object
        let navigationAppearance = UINavigationBar.appearance()
        
        // change color of navigation bar background
        navigationAppearance.barTintColor = UIColor.red
        
        // change color of navigation bar items (buttons)
        navigationAppearance.tintColor = UIColor.white
        
        // change color of navigation bar title
        navigationAppearance.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white];
        
        return true
    }
    
}

Results




iOS Swift. How Programmatically go back to previous ViewController


To go back to Root View Controller:
@IBAction func goToFirst(_ sender: AnyObject) {
        let _ = self.navigationController?.popToRootViewController(animated: true)
    }

To go back to previous View Controller:
@IBAction func showPrev(_ sender: AnyObject) {
        let _ = self.navigationController?.popViewController(animated: true)
    }

Wednesday, October 12, 2016

Algorithms. How to check if two words are anagrams (Java)

What is anagram? 
Any word or phrase that exactly reproduces the letters in another order is an anagram. (Wikipedia)
Example of anagrams: DOG, GOD, DGO, GDO, OGD, ODG, 

Task: 
to determine whether two strings anagrams.

Solution: 
1) make both strings similar case - use toLowesrCase() method 
2) Convert each string to char array
3) Use HashMap - for each character in first char array calculate number of repeats. And after that for second char array for each character decrease count of repeats. So if two string are anagrams than we have to get hash map object where for each character we have zero value.


Source code:
import java.util.HashMap;
import java.util.*;

public class Main {

  static boolean isAnagram(String a, String b) {
  
      HashMap<Character, Integer> hash = new HashMap();
      char[] arr1 = a.toLowerCase().toCharArray();
      char[] arr2 = b.toLowerCase().toCharArray();

      for (char c : arr1) {
        int count = 1;
        if (hash.containsKey(c)) {
          count = hash.get(c) + 1;
        }
        hash.put(c, count);
      }

      for (char c : arr2) {
        int count = -1;
        if (hash.containsKey(c)) {
          count = hash.get(c) - 1;
        }
      hash.put(c, count);
      }

      for (char c : hash.keySet()) {
        if (hash.get(c) != 0) {
          return false;
        }
      }

      return true;
  }

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    String a = scan.next();
    String b = scan.next();
    scan.close();
    boolean ret = isAnagram(a, b);
    System.out.println((ret) ? "Anagrams" : "Not Anagrams");
  }
}



Results:

Test 1:
Hello
olleh
Anagrams

Test 2:
Hello
helll
Not Anagrams

Test 3:
Morning
gninromm
Not Anagrams

Test 4:
Programming
mmggrrpoain
Anagrams

Tuesday, October 11, 2016

iOS Swift. Show two decimal places for Double

We can format output for double values with String Format:
let value = 3.1415
let formatValue = String(format:"%.2f", value) // 3.14

Or we can use NumberFormatter Class (setting min and max number of digits after decimal separator):
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.decimal
formatter.roundingMode = NumberFormatter.RoundingMode.halfUp
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
        
let roundedValue1 = formatter.string(from: 0.684) // 0.68
let roundedValue2 = formatter.string(from: 0.687) // 0.69
let roundedValue3 = formatter.string(from: 0.600) // 0.60


iOS Swift. How to check validity of URL in Swift?

There is system method to check validity of URL -  UIApplication.shared.canOpenURL(url as URL)
import Foundation
import UIKit

extension NSURL {
    func verifyUrl () -> Bool {
        //Check for nil
        if let urlString = self.absoluteString {
            // create NSURL instance
            if let url = NSURL(string: urlString) {
                // check if your application can open the NSURL instance
                return UIApplication.shared.canOpenURL(url as URL)
            }
        }
        return false
    }
}

Monday, October 10, 2016

iOS Swift. Rounding a double value to N number of decimal places

For round double values we can use classic algorithm of rounding. Add new function to Double class (with Extension):
import Foundation
import UIKit

extension Double {
    
    func roundToPlaces(places: Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded() / divisor
    }
    
}

And we can use this extension for double variables:
let pi = 3.14159265
let roundedValue = pi.roundToPlaces(places: 2) // 3.14
print(roundedValue)

How to hide navigation bar for Navigation Controller

If you use Navigation View Controller but want to hide navigation bar you can use following:
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.setNavigationBarHidden(true, animated: false)
}