Question of the sharedStore method


#1

Hi,

I have a question regarding the sharedStore method in the BNRItemStore class.

I noticed at the start the definition of the method, the sharedStore static variable will be assigned the value nil, I am wondering if every time the method is called, will the variable sharedStore be set to nil?

  • (BNRItemStore *)sharedStore
    {
    [color=#FF0000]static BNRItemStore *sharedStore = nil; [/color]
    if (!sharedStore) {
    sharedStore = [[super allocWithZone:nil] init];
    }
    return sharedStore;
    }

Thanks


#2

No. static objects are initialised only once even if they appear inside a method:

+ (BNRItemStore *)sharedStore
{
    static BNRItemStore *sharedStore = nil; 
    if (!sharedStore) {
    sharedStore = [[super allocWithZone:nil] init];
    }
    return sharedStore;
}

The very first time sharedStore method is is invoked [color=#FF0000]sharedStore[/color] static local variable’s value will be nil; thereafter, it won’t be after being set inside the body of the if statement.


#3

Thanks a lot!

[quote=“ibex10”][quote]
… at the start the definition of the method, the sharedStore static variable will be assigned the value nil, I am wondering if every time the method is called, will the variable sharedStore be set to nil?
[/quote]
No. static objects are initialised only once even if they appear inside a method:

+ (BNRItemStore *)sharedStore
{
    static BNRItemStore *sharedStore = nil; 
    if (!sharedStore) {
    sharedStore = [[super allocWithZone:nil] init];
    }
    return sharedStore;
}

The very first time sharedStore method is is invoked [color=#FF0000]sharedStore[/color] static local variable’s value will be nil; thereafter, it won’t be after being set inside the body of the if statement.[/quote]


#4

[quote=“ibex10”][quote]
… at the start the definition of the method, the sharedStore static variable will be assigned the value nil, I am wondering if every time the method is called, will the variable sharedStore be set to nil?
[/quote]
No. static objects are initialised only once even if they appear inside a method:

+ (BNRItemStore *)sharedStore
{
    static BNRItemStore *sharedStore = nil; 
    if (!sharedStore) {
    sharedStore = [[super allocWithZone:nil] init];
    }
    return sharedStore;
}

The very first time sharedStore method is is invoked [color=#FF0000]sharedStore[/color] static local variable’s value will be nil; thereafter, it won’t be after being set inside the body of the if statement.[/quote]

I guess I have a possible dumb question on my side to tag along in this area… but how does !sharedStore result to a true or false of whether it exists or not?


#5

nil is defined as 0: (0 == nil) == true and (!0 == nil) == false

Two cases to consider:
Case 1: Shared store does not exist: (sharedStore == nil) == (nil == nil) == (0 == nil) == true
Case 2: Shared store does exist: (sharedStore == nil) == (!nil == nil) == (!0 == nil) == false


#6

[quote=“ibex10”]nil is defined as 0: (0 == nil) == true and (!0 == nil) == false

Two cases to consider:
Case 1: Shared store does not exist: (sharedStore == nil) == (nil == nil) == (0 == nil) == true
Case 2: Shared store does exist: (sharedStore == nil) == (!nil == nil) == (!0 == nil) == false[/quote]

Thanks for that. I’ve worked with NULL soo many times and have been told it’s NOT 0 and not the same as 0. Having that placed in my head, I saw nil and that’s what popped in my mind. It just took a short google to realize that initializing to nil is initializing to 0.


#7

It took me a while to understand this (as usual) but eventually I grasped that the =nil is not an assignment, it is purely an initialization. Once it is initialized, it is initialized, that’s done and the statement has basically been “used”. I mean that 's the way someone like me with little computing background understood it.


#8

Yes, you could potentially send init to BNRItemStore multiple times. Yes, you could defensively code around the init method to make sure this doesn’t happen. In practice, I wouldn’t worry about it. If I were shipping a library that I intended for people to use (aka, use without reading the documentation), I’d probably worry about it.


#9

Sorry I’m dense but if it’s been initialized and then sharedStore runs again, why doesn’t the first line set sharedStore to nil again?


#10

Thanks for this post, even if it´s little old. So basically the first line of the method is ignored the other times when the method is running?