Contents

Develop
2018.10.31 09:06

[ios] Swift 4 Singleton inheritance

조회 수 1687 댓글 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"



?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
481 Develop Mac OS 에 Jenkins 설치하기 (Homebrew) 2 file hooni 2017.03.15 8385
480 Develop [java] 채팅 프로그램.. swing 사용.. file hooni 2013.04.23 8369
479 Develop [c] 콘솔에서 패스워드 입력시 문자 보이지 않게 하는 코드 file hooni 2013.04.23 8359
478 Develop [c] 암호 알고리즘 소스.. file hooni 2013.04.23 8348
477 Develop [js] selectbox 선택 후 input 박스에 적용 hooni 2013.04.23 8346
476 Develop [js] 쿠키(cookie)에 대한 설명과 예제.. hooni 2003.04.23 8346
475 Develop [swift] NotificationCenter 간단 예제 file hooni 2021.01.27 8333
474 Develop [c] 이진트리(binary tree)의 운행.. hooni 2003.04.23 8299
473 Develop [c++] 레지스트리 편집하는 부분..ㅡㅡ; file hooni 2013.04.23 8298
472 Develop [c++] 현승이가 준 메신저 소스.. ㅋㅋ file hooni 2013.04.23 8296
471 Develop [c] 문자열 정렬 함수 qsort() hooni 2003.04.23 8271
470 Develop [c] selec()를 이용한 입출력 다중화 file hooni 2013.04.23 8271
Board Pagination Prev 1 ... 26 27 28 29 30 31 32 33 34 35 ... 71 Next
/ 71