Contents

Develop
2018.10.31 09:06

[ios] Swift 4 Singleton inheritance

Views 5920 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
541 Develop 다운 받아서 테스트 해볼것.. hooni 2013.04.23 10237
540 Develop [c++] mfc이용한 트레이아이콘(TrayIcon) 클래스 예제 프로젝트 file hooni 2013.04.23 10254
539 Develop [js] jQuery 셀랙터(selector) 요약 hooni 2013.12.17 10266
538 Develop [c] 약수/최대공약수/완전수 알고리즘 hooni 2003.04.23 10271
537 Develop [c] 다중연결 서버 만들기 #2 - select() 사용 file hooni 2013.04.23 10287
536 Develop [java] 스윙(swing)버튼 테스트 ㅋㅋ file hooni 2013.04.23 10310
535 Develop [c] 최대공약수 알고리즘 (유클릿) hooni 2003.04.23 10349
534 Develop 최근 논문 자료 (2011/01/03, 만현형한테 보낸거..) secret hooni 2013.04.23 10366
533 Develop [c] 간단한 채팅(클라이언트/서버) 프로그램 소스 file hooni 2003.04.23 10403
532 Develop [c] 전위 표기법으로 연산 예제.. file hooni 2013.04.23 10404
531 Develop [php] URL/URI 관련 환경변수 hooni 2003.04.23 10422
530 Develop [js] selectbox 선택 후 input 박스에 적용 hooni 2013.04.23 10442
Board Pagination Prev 1 ... 49 50 51 52 53 54 55 56 57 58 ... 99 Next
/ 99