[ios] Swift 4 Singleton inheritance
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"
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
645 | Develop |
[java] 마우스 버튼 테스트.. swing..
![]() |
hooni | 2013.04.23 | 7891 |
644 | Develop | [java] 날짜 계산 (Date, SimpleDateFormat) | hooni | 2013.04.23 | 11858 |
643 | Develop |
[java] 그래픽(도형,다각형..) 이동,확대,축소,회전에 대한 내용..
1 ![]() |
hooni | 2013.04.23 | 8638 |
642 | Develop |
[java] 거스름돈 계산.. swing 사용
![]() |
hooni | 2013.04.23 | 9449 |
641 | System/OS | [java] Tomcat/UTF-8 or All 지원 ㅎㅎ | hooni | 2013.04.23 | 16761 |
640 | Develop | [java] Sieve of Eratosthenes (에라토스테네스의 체) | hooni | 2013.04.23 | 9340 |
639 | Develop | [java] RGB코드를 HEX코드로 변환하는 코드 ㅎㅎ | hooni | 2013.04.23 | 8016 |
638 | Develop |
[java] RGB 색상 조절 버튼.. 스윙(swing)
![]() |
hooni | 2013.04.23 | 7840 |
637 | Develop | [java] netty (비동기 이벤트 방식 네트워크 프레임워크) 사용법 #2 (client) | hooni | 2015.01.02 | 2053 |
636 | Develop | [java] netty (비동기 이벤트 방식 네트워크 프레임워크) 사용법 #1 (server) 1 | hooni | 2015.01.02 | 2228 |
635 | Develop | [java] Interface 와 abstract | hooni | 2013.04.23 | 8709 |
634 | Develop |
[Javascript][Ajax] 자바스크립트 강의 산출물
![]() |
hooni | 2019.10.05 | 715 |