I'm just starting out (reading up a lot for the past couple of days). Here's some questions that I have stacked up, hopefully someone can answer them.
1. the (self != nil) check in initializer code. Why do it? To prevent accidental access to some "run-only-once" code that's wrapped up in there? Where from could this accidental access come from? Doing such checks suggest that I don't have control over what's going on.
- (id)init {
self = [super init]
if (self != nil) {
// Code..
}
return self;
}
2. How is it that you don't have to free up anything that static methods return? (or this is the idea I've got)
3. How is str = @"Hi there!" different from
str = [[NSString alloc] initWithString:@"Hi there!"];
As I understand, you have to release str in aquired with second method, but not with first? If so, when does the first one get released? Which one is preferable (not minding the typing length)?
4. What is autorelease, if iphone has no garbage collection? I've noticed something called "an autorelease pool" being created in main.m. Is [myObject autorelease]; a way of adding myObject to the nearest wrapping "autorelease pool", which will release it? Basically, some magic to avoid releasing it yourself? Why use it?
Well, thats it for now. Thanks for any answers!
Although Objective-C is still supported by Apple, it has never been an open-source language.
Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.
Whenever you create a new file (File > New > File...), you are presented with a list of file templates. Choose Cocoa from the OS X section and select the Objective-C class template to create a new Objective-C class. Click Next to continue. Give the new class a name of Book and set its subclass to NSObject .
If you want to get work done, Objective-C is the way to go. Swift is the new kid on the block, but it's still a kid. If you want to get work done, Objective-C is the way to go. At the time of writing, Apple's software development kits are primarily written in C and Objective-C.
In Objective-C, it's possible to return an instance other than self from -init
. Classes do this, for example, to enforce a singleton instance, or in the case of class clusters. NSNumber
, for example, returns a subclass depending on the type of value passed to its initializer. So when you call [[NSNumber alloc] initWithLong:long_value]
, NSNumber
's -initWithLong:
initializer is called after NSNumber
's +alloc
, but a subclass of NSNumber
may be returned to the oringial caller. Thus the pattern
self = [super init];
which reassigns self
to the value of [super init]
so that self
points to the actual instance that [super init]
returned. If +alloc
or the super's init
method fails, the result of [super init]
may be nil
. To avoid, side effects in the case of a failed initialization, the pattern then becomes
- (id) init { if(self = [super init]) { // do initialization of instance variables etc. } return self; }
Note that you must return self
(or nil
or an other instance) from the init
method. You should assign self to [super init]
and you may check for nil
before doing more work.
You may have to release the return value of a staic method. You should read the Cocoa memory management guide. The rule is generally quite simple: If the method you call has "new", "alloc", or "copy" in its signature, the result belongs to the caller and the caller must call -release
on that instance or there will be a memory leak. Of course you should call -retain
on anything else (i.e. not from an "alloc","new" or "copy" method) you want to keep a reference to and then call -release
or -autorelease
when you are done with that instance.
str = @"Hi there!"
, assuming str
was declared as NSString *str;
assigns the address of the string constant @"Hi there!" to the value of the
strvariable. You do not need to retain or release string constants.
str = [[NSString alloc] initWithString:@"Hi there!"];allocates a new string instance. The value of
strwill be the address of this instance. Each call of
str = [[NSString alloc] initWithString:@"Hi there!"];again will allocate a new instance. So after
str2 = [[NSString alloc] initWithString:@"Hi there!"];,
str != str2, while after
str2 = @"Hi There!", str==str2
. See this answer as well.
-autorelease
adds the receiver to the current NSAutoreleasPool
. When the pool is drained (usually at the end of the current run loop iteration, or when the pool is manually drained), the pool calls -release
on all instances in the pool. If this -release
drops the retain count to 0, the object is deallocated (and -dealloc
called) just as with any other -release
. Using an autorelease pool is generally frowned upon on the iPhone because it may cause you to accumulate many unused instances in the pool before it is drained at the end of the run loop iteration. If you can use -release
instead of -autorelease
, you generally should. Again, see the Cocoa memory management guide for more info.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With