Contents

Develop
2018.10.31 09:06

[ios] Swift 4 Singleton inheritance

Views 1696 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
481 Develop [c] 게임 AI FSM 테스트 샘플 소스.. 꽤 괜찮은 소스.. file hooni 2013.04.23 7403
480 Develop [asp] 폼 메일 소스 file hooni 2013.04.23 7403
479 Develop [js] 빈도우즈(bindows96) file hooni 2013.04.23 7403
478 Develop [c] 문자열 치환해주는 str_replace() 함수 file hooni 2013.04.23 7415
477 Develop [c] KNN(K-nearest neighbor) 패턴인식 과제.. file hooni 2013.04.23 7417
476 Develop [c] 농구팀 점수 산출 프로그램 소스 file hooni 2003.04.23 7417
475 Develop [jsp][php] LDAP 프로그래밍.. file hooni 2003.04.23 7419
474 Develop [php] 무조건 다운로드 받도록 header 세팅 file hooni 2013.04.23 7427
473 Develop [c] 시간(요일,날짜 포함) 출력하는 프로그램 초간단 코드 hooni 2013.04.23 7428
472 Develop [c++] mfc로 만든 월플렉스 멀티 수납 시스템(2D기반 설계) file hooni 2013.04.23 7429
471 Develop [js] 자바스크립트 메뉴얼 사이트.. ㅋㅋ hooni 2013.04.23 7436
470 Develop [c] kmp 활용 search file hooni 2013.04.23 7439
Board Pagination Prev 1 ... 26 27 28 29 30 31 32 33 34 35 ... 71 Next
/ 71