Contents

Develop
2018.10.31 09:06

[ios] Swift 4 Singleton inheritance

Views 1695 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
201 Develop [ios] 동영상 플레이어 샘플 (for Remote Url) file hooni 2017.02.07 1767
200 Develop [web] 더 빠른 웹을 위한 프로토콜, 'HTTP/2' file hooni 2014.10.20 1767
199 System/OS 네트워크 용어 정리 file hooni 2022.11.20 1755
198 System/OS [mac] Mac OS에서 재생되는 사운드를 녹음하는 방법 file hooni 2016.10.03 1746
197 Develop [ios] Locale Identifiers file hooni 2018.11.29 1744
196 Develop [ios] Requesting Location Permissions in iOS file hooni 2018.08.18 1742
195 System/OS Enable Safari Hidden Debug Menu in Mac OS X file hooni 2017.02.07 1734
194 Develop ZBar 라이브러리를 이용한 바코드 스캔 앱 개발하기 file hooni 2015.01.01 1730
193 System/OS [svn] SVN trunk 변경사항 되돌리기 (SVN Rollback) hooni 2014.11.27 1717
192 Develop [python] 파라미터 앞에 *, ** 의 의미? (*args, **kwargs) hooni 2019.11.22 1703
191 System/OS 무료로 HTTPS 적용하기 (Let's Encrypt) file hooni 2017.10.28 1701
190 Develop [js] 모바일 스크롤 방지(해제) hooni 2015.04.14 1698
Board Pagination Prev 1 ... 77 78 79 80 81 82 83 84 85 86 ... 98 Next
/ 98