Develop
2018.10.31 09:06

[ios] Swift 4 Singleton inheritance

Views 1592 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
1125 System/OS [linux] 쉘 스크립트에 대한 설명과 예제.. hooni 2003.04.23 13413
1124 System/OS asx미디어 정보 기록.. hooni 2003.04.23 20533
1123 System/OS 컴파일러 수업 자료(교재 : 컴파일러 입문) file hooni 2003.04.23 21966
1122 Develop [c] 민수형 libipq 샘플 소스 ㅋㅋ hooni 2003.04.23 14112
1121 Develop [php] 니우쪽지다.. 받아라~ ^^ file hooni 2003.04.23 9834
1120 System/OS [perl] 펄 환경 설정해야 할거 ㅋㅋ hooni 2003.04.23 14992
1119 Etc [htm] DOM에 대해 ㅎㅎ file hooni 2003.04.23 14717
1118 Develop [c++]현승이가 보내준 동영상 암호화 자료.. 볼것.. file hooni 2003.04.23 9728
1117 Develop [c++]현승이가 보내준 암호화 모듈 AES라인델.. file hooni 2003.04.23 10714
1116 Develop [php] 배열 관련 함수 설명 ㅎㅎ hooni 2003.04.23 11746
1115 Develop [css] 화면 스크롤 제어 ㅋㅋ hooni 2003.04.23 8681
1114 Develop [js] 자바스크립트 이벤트 핸들.. hooni 2003.04.23 7908
1113 Develop [php] 쉘에서 실행할 때 인수(파라미터) 받기.. hooni 2003.04.23 11588
1112 Develop [js] 자바스크립트로 응용프로그램 실행 ㅎㅎ hooni 2003.04.23 9812
1111 Develop [php] URL/URI 관련 환경변수 hooni 2003.04.23 9147
1110 Develop [js] 쿠키(cookie)에 대한 설명과 예제.. hooni 2003.04.23 8321
Board Pagination Prev 1 2 3 4 5 6 ... 74 Next
/ 74