2

here is a simple SingleTon pattern in Swift, it comes from:

https://github.com/hpique/SwiftSingleton

class Test {
    static let shareTest = Test()
    var a = 1
    init() {
        println("testSingeleTon")
    }
}

and here is a test function:

func testFunc() {

        var s1 = Test.shareTest
        var s2 = Test.shareTest
        var s3 = Test.shareTest
        var s4 = Test.shareTest

        func printPointer(pointer: UnsafePointer<Test>) {
            println(pointer)
        }

        printPointer(&s1)
        printPointer(&s2)
        printPointer(&s4)
        printPointer(&s3)

        println(s1.a)
        println(s2.a)
        println(s3.a)
        println(s4.a)

        s1.a = 4444

        println("s2.a = \(s2.a)")
        println("s3.a = \(s3.a)")
        println("s4.a = \(s4.a)")

    }

and what i confused is the result:

testSingeleTon
0x00007fff54432ad8
0x00007fff54432ad0
0x00007fff54432ac0
0x00007fff54432ac8
1
1
1
1
s2.a = 4444
s3.a = 4444
s4.a = 4444

it looks like a singleTon pattern, because just assign the value of s1.a, than the s2.a, s3.a, s4.a value had changed too, but, if it is a really singleTon pattern,why the &s1, &s2, &s3, &s4 are totally different??

0x00007fff54432ad8
0x00007fff54432ad0
0x00007fff54432ac0
0x00007fff54432ac8
A BLUE
  • 33
  • 4
  • actually you are not printing the value of the s1,s2,s3,s4..you are printing the the memory location for s1,s2,s3 and s4 using & sign....remember those memory contains the same instance – LC 웃 Jun 30 '15 at 16:00
  • @anishparajuli, if you want credit for the correct answer you should post this as an answer rather than a comment, and then the OP should accept it. – Duncan C Jun 30 '15 at 16:14
  • ok..thanks i did it :) – LC 웃 Jun 30 '15 at 16:29

3 Answers3

2

Well,you are not printing the value of the s1,s2,s3,s4..you are printing the the memory location for s1,s2,s3 and s4 using & operator....

And remember those memory location contains the same instance.....So you are well with your singleton pattern

LC 웃
  • 18,888
  • 9
  • 57
  • 72
1

Each variable, s1, s2, s3, s4 hold a pointer to a Test object. Printing &s1 prints the location of that pointer, not the location of the object pointed to. If you want to print the address of an object that an object reference points to

println(unsafeAddressOf(s1))
println(unsafeAddressOf(s2))

etc.

source

source2 (Question I asked in order to figure this out better)

Community
  • 1
  • 1
Will M.
  • 1,864
  • 17
  • 28
  • is this what you asked... go and check your question??its crazy @ABLUE –  Jul 01 '15 at 11:19
  • :( sorry...this is my first question, my description not clear enough, i will improve next time, sorry again...@copeME – A BLUE Jul 01 '15 at 13:52
  • @copeME I answered like the other answers but more clearly and I provided a way to for OP to do what he wanted his program to do, which was also a way to show that the variables held the same Test, instead of just saying that is what was what was happening. – Will M. Jul 01 '15 at 14:16
  • @ABLUE just be fair..this is how things work in SO... anyway try to learn then –  Jul 01 '15 at 14:37
  • @WillM. you posted the answer but the later one he was confused on...not the one he asked on the question...its ok!!! –  Jul 01 '15 at 14:40
  • @copeME I answered both questions. – Will M. Jul 01 '15 at 14:40
  • @WillM. perhpas ,you were not the first one to answer ? –  Jul 01 '15 at 14:42
  • @copeME "Accepting an answer is not meant to be a definitive and final statement indicating that the question has now been answered perfectly. It simply means that the author received an answer that worked for him or her personally" – Will M. Jul 01 '15 at 14:48
0

Singletons before Swift 1.2 Thanks to Will M. for clarification.

class ActivityIndicator {

    class var sharedInstance: ActivityIndicator {
        struct Singleton {
            static let instance = ActivityIndicator()
        }
        return Singleton.instance
    }
}
Rasputin
  • 487
  • 4
  • 10
  • thanks for answer, i had tried it, it works, but :) , it has the same problem, the result of &s1, &s2, &s3, &s4, are different : `0x00007fff5f8dcad8 0x00007fff5f8dcad0 0x00007fff5f8dcac0 0x00007fff5f8dcac8 1 1 1 1 s2.a = 4444 s3.a = 4444 s4.a = 4444` – A BLUE Jun 30 '15 at 16:03
  • http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift you don't need to do that implementation in Swift 1.2 and above. – Will M. Jun 30 '15 at 16:04