3

Following my previous post about UIViewControllers designated initializer initWithCoder I have another question about the argument aDecoder passed into the protocol method.

Here is the code in question:

@implementation WhereamiViewController

- (id)initWithCoder:(NSCoder *)aDecoder //we're overiding the superclasses (UIViewController) inititalizer
{
    self = [super initWithCoder:aDecoder];

    if (self){
        //create location manager object
        locationManager = [[CLLocationManager alloc] init];

        //there will be a warning from this line of code
        [locationManager setDelegate:self];

        //and we want it to be as accurate as possible
        //regardless of how much time/power it takes
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

        //tell our manager to start looking for its location immediately
        [locationManager startUpdatingLocation];
    }

    return self;
}

I was curious about aDecoder so renamed it to see if my code would still work and it did. What I want to know is what exactly is being passed into initWithCoder as an argument? It seems like nothing is in my code. Is the argument just part of the method and has to be shown even if nothings being passed into it? Other times I have created designated initializers I've done it like this:

self = [super init]

init is NSObjects designated initializer right?

This is the only part of the code I don't understand. I see I'm calling my superclasses initializer and then implementing it with additional custom code / making it selfs (whereamiviewcontroller) value.

I did set a marker and look in the logs to see if anything would catch my eye but had no luck with that.

Thanks in advance
Regards

2
  • Did you read the documentation? See the docs for the NSCoding protocol. Commented Oct 17, 2013 at 23:40
  • I have quite a few times and maybe I missed something. I know NSCoding has 2 methods and one of them is initWithCoder. It says that aDecoder is an unarchiver of objects. Commented Oct 17, 2013 at 23:45

1 Answer 1

2

You can see the -initWithCoder: method in action when you try to initialise your view controller instance from nib or storyboard. In this case Cocoa Touch will decode the controller elements from the xml using UINibDecoder instance by -initWithCoder: initialiser.

Sign up to request clarification or add additional context in comments.

2 Comments

I see the UINibDecoder being set as value for aDecoder in my logs when the complier reaches this line: self = [super initWithCoder:aDecoder];. How is anything being passed into initWithCoder as an argument? Surely this is happening behind the scenes.
@LondonGuy yes, the UINibDecoder is a private class based on NSCoder class so the magic is out of our scope. But there are some runtime exploration results that can help you to get deeper into it: github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.