简体   繁体   中英

Must a property be alloc'ed and init'ed if a pointer to an object?

I am back with just one final specific question regarding properties. I understand that properties will be initialised to either zero or nil upon instantiation of a class.

This may seem like a stupid question but I would like to ask as I am just not 100% certain. Is it the case that a property that is not a primitive type and is a pointer to an object such as an NSArray object would need to be alloced and inited in the implementation of a class in order to be usable?

And that if not alloced and inited, it would just remain nil and unused?

I have read many questions on here and researched online, etc and just can't find a definitive answer to this, added to that I am just confused about it and would really appreciate if someone could clarify this.

Thanks to anyone who responds!

To add to Gal's answer...

If you create an object like...

@property SomeClass *someObject; // this is nil

Then you try to add it to an array or dictionary...

NSDictionary *dictionary = @{"Object" : self.someObject};

This will crash at run time.

Just because an object is nil does not mean that it will never be used. It means that it is nil. When you use it it will be nil until it is instantiated.

However, you don't have to instantiate it at init of the parent object.

I regularly use lazy initialisation like...

- (NSObject *)someObject {
    if (!_someObject) {
        _someObject = [SomeClass new];
    }
    return _someObject;
}

With this method in place it means that the object will be nil until it is first accessed (with self.someObject ). On that first access the object will be initialised and then returned.

This means that the code above (putting it into the dictionary) will no longer crash as the object will not be nil when it is put into the dictionary.

When you define NSArray * or any other non primitive object you it be occupied with a nil. For example:

 NSObject *obj; //This object is nil at this stage

If you don't initialise it the object will remain nil. In objective-c every call to nil will yield nil. So if you call [obj doSomething] at this stage it won't,it will return nil.

In order to initialize it you have to allocate it. Calling alloc only would not be correct. alloc zeroes out all instance variables of the object, init then has the chance to set all or some instance variables to their default values. Some classes even use their init methods to create another instance and return that one instead of the one you allocated.

Many classes expect that their init methods get called and would possibly cause crashes if you don't call init. If you are talking about a custom class that inherits directly from NSObject and needs no initializations of instance variables, you might get away with [myClass alloc] but it is definitely not good programming style.

A pointer is really only useful when it points to something. An object property is just a pointer to an object (also called a reference to said object). To have a useful reference, you just need to point it somewhere. In C (and by extension, Objective-C), this is done via assignment.

myProperty = ...;

Creating a new class instance is just the process of allocating memory and initializing it. The idiom in Objective-C is the alloc+init pair, though it should be noted that init is just the basic version. Many/most classes have a variety of utility init methods that take relevant parameters. There are also so-called factory methods (eg stringWithFormat: ) that call alloc and init (in this case initWithFormat: ) for you. The new method, for example, just calls alloc and init .

All that said, all init methods return a reference to the (new) instance which was just initialized. When used in an assignment, you're just saving that reference into your property.

That's one way to get a property set to a useful value. The other is to simply assign a reference to an existing instance, which was alloc ed and init ed at some point in the past.

myClass.myProperty = someValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM