Contents

Develop
2018.10.31 09:06

[ios] Swift 4 Singleton inheritance

조회 수 1578 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

Swift 4 에서 싱글톤 객체를 사용하는 방법은 여러 가지가 있겠지만 이건 내가 자주 사용하는 두 가지..


# 변수 형태로 사용하기

ex) Singleton.shared.name = "Hello"

클래스를 상속 받아서 확장하기 어려움 ㅠㅠ


import UIKit


class Singleton

{

    static let shared: Singleton = {

        var instance = Singleton()

        instance.name = "default name"

        return instance

    }()

    

    var name: String = String()

}


class SingletonSubclass: Singleton {

    static var sharedSub: SingletonSubclass = {

        var instance = SingletonSubclass()

        instance.name = "default name"

        return instance

    }()

    

    var title: String = String()

}


Singleton.shared.name          // ""

SingletonSubclass.shared.name  // ""


SingletonSubclass.shared.name = "haha"

SingletonSubclass.sharedSub.title = "hello"


Singleton.shared.name             // ""

//Singleton.shared.title          // (error)

SingletonSubclass.shared.name     // "haha"

SingletonSubclass.sharedSub.title // "hello"




# 함수 형태로 사용하기

ex) Singleton.sharedInstance().name = "Hello"

클래스를 상속 받아 확장하기 편함

Sometimes you need to subclass your singleton…


import UIKit


class Singleton

{

    class func sharedInstance() -> Singleton {

        struct inner { static let instance = Singleton() }

        inner.instance

        return inner.instance

    }

    

    var name: String = String()

}


class SingletonSubclass: Singleton

{

    override class func sharedInstance() -> SingletonSubclass {

        struct inner { static let instance = SingletonSubclass() }

        return inner.instance

    }

    

    var title: String = String()

}


Singleton.sharedInstance().name          // ""

SingletonSubclass.sharedInstance().name  // ""


SingletonSubclass.sharedInstance().name = "haha"

SingletonSubclass.sharedInstance().title = "hello"


Singleton.sharedInstance().name          // ""

//Singleton.sharedInstance().title       // (error)

SingletonSubclass.sharedInstance().name  // "haha"

SingletonSubclass.sharedInstance().title // "hello"



?

  1. [ppt] 정보보호관리 발표내용 #2

    Date2016.12.08 CategoryPPT Byhooni Views1042
    Read More
  2. [ppt] 정보보호관리 발표내용

    Date2016.11.30 CategoryPPT Byhooni Views1052
    Read More
  3. [ppt] 보안경제학 발표자료

    Date2016.11.11 CategoryPPT Byhooni Views5
    Read More
  4. 삼성페이(Samsung Pay) 구조

    Date2016.09.09 CategoryPPT Byhooni Views903
    Read More
  5. [ppt] 전자금융보안론 발표/설치 자료

    Date2016.05.03 CategoryPPT Byhooni Views1289
    Read More
  6. [ppt] OCB기술개발팀 OJT 자료

    Date2015.11.13 CategoryPPT Byhooni Views1131
    Read More
  7. [ppt] Macro for board game 발표자료 (@Team Study 2013.01.18)

    Date2015.07.22 CategoryPPT Byhooni Views1245
    Read More
  8. [ppt] Information Security 발표 자료 (@Team Study 2012.11.15)

    Date2015.07.22 CategoryPPT Byhooni Views859
    Read More
  9. [ppt] Equation Solving 발표 자료 (@AjaxUI랩 밋업데이 2012.02.28)

    Date2015.07.22 CategoryPPT Byhooni Views885
    Read More
  10. [ppt] iOS 플라랩#03(2015.04.27) 발표 자료

    Date2015.04.24 CategoryPPT Byhooni Views1048
    Read More
  11. [ppt] iOS 플라랩#02(2015.03.19) 발표 자료

    Date2015.04.24 CategoryPPT Byhooni Views889
    Read More
  12. [ppt] iOS 플라랩#01(2015.02.26) 발표 자료

    Date2015.02.25 CategoryPPT Byhooni Views708
    Read More
Board Pagination Prev 1 2 3 Next
/ 3