fncomp
fncomp

Reputation: 6188

Initializing custom objects inside delegate

I've created a text field using storyboard and am trying to setup the delegates. The process looks straightforward and the callback is called when I expect, but my custom object, which I attempt to initialize in the callback, doesn't seem to initialize. I believe the relevant bits of code are in the ViewController for the view that contains the text field and in the custom object.

Oddly, in the debugger, I see a property at self.feed that has the right methods, but I can't seem to log from within its initializer, which has me quite confused.

Here's the callback for the text field:

-(BOOL) textFieldShouldReturn: (UITextField *)textField {    
    NSLog(@"Begin feeding."); // This logs

    NSURL *path = [NSURL URLWithString: @"http://feeds2.feedburner.com/hackaday/LgoM"];
    self.feed = [[Feed alloc] initWithContentsOfURL: path];

    NSLog(@"Should have a feed."); // So, does this.
    [textField resignFirstResponder];

    return YES;
}

Here's the Feed interface:

#import <Foundation/Foundation.h>
#import "FeedItem.h"
#import "FeedParser.h"
#import "FeedParserDelegate.h"

@interface Feed : NSMutableArray <NSURLConnectionDelegate, FeedParserDelegate>{
    BOOL isValidFeed;
    FeedParser *parser;
    NSMutableData *xmlData;
    NSURLConnection *connection;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSURL *link;
@property (nonatomic, retain) NSURL *url;
@property (nonatomic, retain) NSString *description;

- (id) initWithUrl: (NSURL *) url; 
- (BOOL) isValidFeed;

- (void) _populate;
- (void) _fetchData;

@end

And finally the initializer I'm hoping will be called, but doesn't seem to be:

#import "Feed.h"

@implementation Feed

@synthesize name;
@synthesize link;
@synthesize url;
@synthesize description;


- (id) initWithUrl: (NSURL *) feedUrl{
    NSLog(@"initing with NSUrl"); // This never logs.
    self = [super init];

    if (self) {
        self.url = feedUrl;
        [self _populate];
    }

    return self;
}

Essentially, my desired outcome is to get initing with NSUrl to log to the console.

Any help would be greatly appreciated. Please, let me know if there are other relevant details I should post (I'm obviously new to iOS).

Upvotes: 0

Views: 164

Answers (1)

Flyingdiver
Flyingdiver

Reputation: 2142

You're calling initWithContentsOfURL:, but you've implemented initWithUrl:. Aren't you getting a compiler warning?

Upvotes: 1

Related Questions