Develop
2018.10.31 09:06

[ios] Swift 4 Singleton inheritance

Views 1588 Votes 0 Comment 0
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print

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
No. Category Subject Author Date Views
825 Develop [php] whois정보 조회 프로그램 hooni 2003.04.23 11733
824 Develop [c] 최대공약수 알고리즘 (유클릿) hooni 2003.04.23 8873
823 Develop [c] 소수점 반올림^^ hooni 2003.04.23 8478
822 Develop [c] 하노이탑 - 재귀함수 hooni 2003.04.23 9153
821 Develop [c] 팩토리얼.. - 재귀함수 hooni 2003.04.23 9500
820 Develop [c] 간단한 채팅(클라이언트/서버) 프로그램 소스 file hooni 2003.04.23 9068
819 Develop [c] 그래픽스 자료(OpenGL 라이브러리) 샘플 소스 file hooni 2003.04.23 10181
818 Develop [opengl] 컴퓨터 그래픽스 강의 자료(수업자료) file hooni 2003.04.23 20432
817 Develop [c++] p.47 연습문제 3번 hooni 2003.04.23 10105
816 Develop [c++] p.58 연습문제 2번 hooni 2003.04.23 16296
815 Develop [c++] p.118 확인학습 5번 hooni 2003.04.23 10734
814 Develop [c] 프로그래밍 ppt, 스킬업 (비트 수업자료) file hooni 2003.04.23 8177
813 Develop [css] 스크롤바 안생기게 hooni 2003.04.23 9886
812 Develop [c] 민수형 libipq 샘플 소스 ㅋㅋ hooni 2003.04.23 14112
811 Develop [php] 니우쪽지다.. 받아라~ ^^ file hooni 2003.04.23 9834
810 Develop [c++]현승이가 보내준 동영상 암호화 자료.. 볼것.. file hooni 2003.04.23 9728
Board Pagination Prev 1 2 3 4 5 ... 53 Next
/ 53