페이지

2020년 12월 4일 금요일

yield return, yield break

public static IEnumerable<char> AlphabetSubset2(char start, char end)
{
char[] chars = new char[end - start];
int i = 0;
for (var c = start; c < end; c++)
{
chars[i++] = c;
}

return chars;
}


public static IEnumerable<char> AlphabetSubset(char start, char end)
{
for (var c = start; c < end; c++)
yield return c;
}

2020년 11월 18일 수요일

Setting subViews's constraints in UIScrollView correctly

import SnapKit

 

func setupUI(){


        
view.addSubview(scrollView)

        scrollView.snp.makeConstraints { (mkr) in

            mkr.left.right.top.bottom.equalTo(self.view.safeAreaLayoutGuide)

        }

     

        let view = UIView()

        view.backgroundColor = .yellow

        scrollView.addSubview(view)

        view.snp.makeConstraints{

            $0.left.equalTo(10)

            $0.right.equalTo(10)

            $0.top.equalTo(10)

            $0.height.equalTo(100)

        }


        let view2 = UIView()

        view2.backgroundColor = .green

        scrollView.addSubview(view2)

        view2.snp.makeConstraints{

            $0.width.height.equalTo(200)

            $0.centerX.equalTo(scrollView)

            $0.top.equalTo(10+100+10)

        }

        let view3 = UIView()

        view3.backgroundColor = .orange

        scrollView.addSubview(view3)

        view3.snp.makeConstraints{

            $0.width.height.equalTo(300)

            $0.left.equalTo(10)

            $0.top.equalTo(10+100+10+200+10 )

        }


        scrollView.contentSize = CGSize(width: view.frame.size.width, height:1000)




}


Scrollable content size is ambiguous for UIScrollView

>> instead, Set widthAnchor.



















2020년 10월 8일 목요일

DispatchQueue async, asynAfter

 DispatchQueue.main.async {

            print("async !")

        }




func delay(interval: TimeInterval, closure: @escaping () -> Void) {

         DispatchQueue.main.asyncAfter(deadline: .now() + interval) {

              closure()

         }

    }


2020년 9월 22일 화요일

UITableViewCell insect

 


class NoticeCell: UITableViewCell {

    

    override func layoutSubviews() {

        super.layoutSubviews()

        contentView.frame = contentView.frame.inset(by: UIEdgeInsets(top: 5, left: 20, bottom: 5, right: 20))


    }


2020년 7월 27일 월요일

usage closure


            let button :UIButton = { ()->UIButton in

                let temp = UIButton()

                return temp

            }()

2020년 3월 11일 수요일

Change navigation back button image



override func viewDidLoad() {
    super.viewDidLoad()
    let backBtn = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
    backBtn.setBackButtonBackgroundImage(UIImage(named:"myBackButton"), for: .normal, barMetrics: .default)
    navigationItem.backBarButtonItem = backBtn
    navigationController?.navigationBar.backIndicatorImage = UIImage()
    navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage()

}

2020년 3월 10일 화요일

UINavigationBar Height change


class MyNavigationController : UINavigationController {
    override func viewDidLoad(){
        super.viewDidLoad()
        self.additionalSafeAreaInsets.top = 70 // not including statusBar (44pt), navigationBar (44pt)    
    }
}



2020년 3월 6일 금요일

push vs present



class ViewController:UIViewController {

     ....

     func showPopup(){
            let popupVc = AppDelegate.shared.getVC( "\(PopupViewController.self)")

             // 1. pushViewController
             //self.navigationController?.pushViewController( popupVc,  animated:true)

             // 2. present ViewController

             //self.present( popupVc, animated:true, completion:nil )
     }
}


1. pushViewController

ViewController disappeared. but navigationController manages this ViewController.




2. present ViewController

ViewController didn't disappear.
PopupViewController is stacked on the ViewController.
UIWindow has 2 UITransitionViews ( navigationController , PopupViewController )

2020년 2월 9일 일요일

Swift ?? operator, Characters

// 1. ?? operator
var a : Int?
let c : Int?
var b : Int = 123

var ss = a ?? b
print(ss)
ss = c ?? b // error : constant c is used before being initialized.
print(ss)
a = nil
ss = a ?? b
print(ss)




// 2. Working with Characters
for character in "Dog!^^".characters {
    print(character)
}



let catCharacters: [Character] = ["C","a","t","!"]
let catString = String(catCharacters)
print(catString)

var tmp = catString
tmp.append("?")
print(tmp)


Swift collection: array, set,dictionary

// 1. Collection : Array
var a = [Int]()
var b = Array(repeating: 4, count: 3)
print(a)
print(b)
a = [1,2,3]
var c = a + b
print(c)
c += [5,6]
print(c)

c[2...5] = [9,10]  //it's brilliant way.
print(c)
for (index, val) in c.enumerated(){
    print( "\(index) => \(val)" )
}



// 2. Collection : Set
var even:Set<Int> = [2,4,6,8,10]
var odd:Set = [1,3,5,7,9]
var tmp = Set<Int>()
even.insert(12)
tmp = [2,3,4,5]

print(tmp.intersection(odd))
print(tmp.union(even))
print(tmp.symmetricDifference(even).sorted())























//3. Collection : Dictionary
var dict = [Int:String]()
dict = [:]
print(dict)

var airportCode = ["a":"america", "b":"bangkok", "c":"china"]
for( key, value ) in airportCode {
    print( "key:\(key) val:\(value)")
}




Swift loop, switch,fallthrough

//1. loop

for i in 0...3 {
    print(i)
}

for i in (0...3).reversed(){
    print(i)
}


//2. switch interval matching
let a = 32
switch a {
case 0,3:
    print("0,3")
case 0...19:
    print("...19")
case 20..<40:
    print("20..<40")
default:
    print("default")
}





// switch tuples

let a = (1, 1)
switch a {
case (0, 0):
    print("(0, 0) is at the origin")
case (_, 0):
    print("(\(a.0), 0) is on the x-axis")
case (0, _):
    print("(0, \(a.1)) is on the y-axis")
case (-2...2, -2...2):
    print("(\(a.0), \(a.1)) is inside the box")
default:
    print("(\(a.0), \(a.1)) is outside of the box")

}





// switch value binding

let a = (2, 0)
switch a {
case (let x, 0):
    print("on the x-axis with an x value of \(x)")
case (0, let y):
    print("on the y-axis with a y value of \(y)")
case let (x, y):
    print("somewhere else at (\(x), \(y))")
case (let x,0),(0, let x):       
    //warning: case will never be executed
    print("On an axis, \(x) from the origin")
case (let x,0),(0, let y) :      
    //error: x,y must be bound in every pattern
    print("error")

}





//switch where

let a = (101,10)
switch a {
case let(x,y) where x == y :
    print("x==y")
case let(x,y) where x >= y :
    print("x>=y")
case let(x,y) where x <= y :
    print("x<=y")
default:
    print("default")
}



// switch fallthrough

var a = 1
switch a {
    case 0...4:
        print("0...4")
        fallthrough
    case 5...10:
        print("5....10")
    case 11...14:
        print("11...14")
    default:
        print("default")
}




Light Normal Bias

Custom View , @IBDesignable, @IBInspectable


//BaseViewWithXib.swift
import UIKit

class BaseViewWithXib: UIView {
    var contentView: UIView?
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupView()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupView()
    }

    

    func setupView() {
        let view = viewFromNibForClass()
        view.frame = bounds

        view.autoresizingMask = [
            UIViewAutoresizing.flexibleWidth,
            UIViewAutoresizing.flexibleHeight
        ]

        addSubview(view)
        self.contentView = view
    }


    private func viewFromNibForClass() -> UIView {
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        return view
    }

}





//MyView.swift
import UIKit

@IBDesignable class MyView: BaseViewWithXib {

    @IBInspectable var bgColor:UIColor?{
        didSet {
            self.contentView?.backgroundColor = bgColor
        }
    }


    //must be . if not, IB designable compile error occur!
    override init(frame: CGRect) {
        super.init(frame: frame)     
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

   
}


2020년 1월 31일 금요일

How to install old OSX ( El Capitan )

1. download El capitan OSX


https://support.apple.com/en-us/HT206886

* execute Install dmg and install.pkcg file. and "Install El capitan.app" file  will be placed in /Applications/


2. create a bootable usb from "Install El capitan.app" 


https://support.apple.com/en-me/HT201372


3. Install OSX


if error message "This copy of the install OS X ... application can't be verified. It may have been corrupted or tampered with during downloading." occur during installing osx,
check your system date on your mac.




* checking date
Open Terminal and Type date

if date is not current time, change date.




* change date
type date month day time minute year

date 0201010120

2/1/2020, 01:01

2020년 1월 14일 화요일

rootViewController is the first ViewController on Application stack


var firstNavigationController = UINavigationController(rootViewController: self.mainViewController )
window?.rootViewController = firstNavigationController


PopupViewController #3 : PopupViewController Style

override func viewWillAppear(_ animated: Bool) {

    super.viewWillAppear(animated)
    self.view.backgroundColor = UIColor(white: 0, alpha: 0.5)
    self.modalTransitionStyle = .crossDissolve
    self.modalPresentationStyle = .overCurrentContext
}

Add InitViewController

In App executing at first time, app sometimes have lots of tasks.


  • splash view showing
  • check app version
  • load app significant values from UserStandard or local storage
  • showing app usage info popup view ( it's a one-off )
  • locked screen for log-in
  • and so on
  • splash view hiding


class AppDelegate: UIResponder, UIApplicationDelegate {
    var window :UIWindow?
    var naviController :UINavigationController?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        naviController = window?.rootViewController as? UINavigationController ;
        showSplash()
    }

    func showSplash(){
        let vc = UIViewController.instantiateViewController(storyboard: "Sotryboard", viewController: "\(InitViewController.self)" )
        window?.rootViewController = vc;
    }

}





class InitViewController: UIViewController{
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        checkInit()
    }


    func checkInit(){
        Async.serial(tasks: [
        {  done in
            task1 {
                done(nil)
            }
        }
        ,
        { done in
            task2 {
                done(nil)
            }
        }
        ,
        { done in
            //chage rootViewController.
            UIApplication.appDelegate?.window?.rootViewController = UIApplication.appDelegate?.naviController
            self.dismiss(animated: true, completion: {
                    done(nil)
            })
        }

    }

}



PopupViewController #2 : PopupViewController must check if another popupView is presenting


if let vc = UIApplication.rootViewController?.presentedViewController {
    vc.dismiss(animated: false) {
        UIApplication.rootViewController?.present( myPopupVc, animated: true, completion: nil )
    }
} else {
    UIApplication.rootViewController?.present( myPopupVc, animated: true, completion: nil)
}

PopupViewController #1 : use rootViewController.present when presenting Pop Up ViewController

When we are going to show Custom Popup Style ViewController, It's good to use rootViewController.present(,,,, )  method.

.rootViewController?.present( myPopupViewController, animated: true, completion: nil )

Reason 1.

self.present( myPopupViewController, animated: true, completion: nil )
    //self : SomeViewController


if self(SomeViewController) has dismissed while presenting myPopupViewController, myPopupViewController also will be dismissed without user's interaction.