Ponchotg
Ponchotg

Reputation: 1195

Xcode: Pass NSString from one class to another problem

OK this should be an easy one but still im breaking my head here:

In my root view controller I have a NSString called "entry" and is working perfectly. I NSLogged it and it works. I have another class called ´ParseOperation´ and in it i have a NSStringcalled "localEntry" and im trying to send to "ParseOperation" the variable "entry" from "RootViewController" this is my RootViewController code for that:

RootViewController.m  

ParseOperation *parseOperation = [[ParseOperation alloc] init];  
parseOperation.localEntry = entry;

It just doesn't work. If I NSLog in my ParseOperation.m it returns "null", but if i do it on my RootViewController it returns the correct variable. and yes i did imported the ParseOperation.h

Here is the ParseOperation code (only the part that uses localEntry):

ParseOperation.h  

@interface ParseOperation : NSOperation <NSXMLParserDelegate>
{
    NSString        *localEntry;
}
@property (nonatomic, retain) NSString *localEntry;

@end

ParseOperation.m  

@synthesize localEntry;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
                                    namespaceURI:(NSString *)namespaceURI
                                   qualifiedName:(NSString *)qName
                                      attributes:(NSDictionary *)attributeDict
{

    //NSLog(@"entrada %@",localEntry);
    if ([elementName isEqualToString:localEntry])
    {
        self.workingEntry = [[[AppRecord alloc] init] autorelease];
    }
    storingCharacterData = [elementsToParse containsObject:elementName];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
                                  namespaceURI:(NSString *)namespaceURI
                                 qualifiedName:(NSString *)qName
{
    if (self.workingEntry)
    {
        if (storingCharacterData)
        {
            NSString *trimmedString = [workingPropertyString     stringByTrimmingCharactersInSet:
                                   [NSCharacterSet whitespaceAndNewlineCharacterSet]];
            [workingPropertyString setString:@""];  // clear the string for next time
            if ([elementName isEqualToString:kIDStr])
            {
                self.workingEntry.appURLString = trimmedString;
            }
            else if ([elementName isEqualToString:kNameStr])
            {        
                self.workingEntry.appName = trimmedString;
            }
            else if ([elementName isEqualToString:kImageStr])
            {
                self.workingEntry.imageURLString = trimmedString;
            }
            else if ([elementName isEqualToString:kArtistStr])
            {
                self.workingEntry.artist = trimmedString;
            }
        }
        else if ([elementName isEqualToString:localEntry])
        {
            [self.workingArray addObject:self.workingEntry];  
            self.workingEntry = nil;
        }
    }
}

THANKS!

Upvotes: 1

Views: 4153

Answers (3)

Ponchotg
Ponchotg

Reputation: 1195

To answer my own question I just had to connect the viewController and the ParseOperation programmatically by adding the following to my header in the parseOperation:

@class RootViewController;

RootViewController *rootViewController;

@property (nonatomic, retain) IBOutlet RootViewController *rootViewController;

And the following on the m file of the ParseOperation:

#import "RootViewController.h"

rootViewController = [[RootViewController alloc]init];

After that in the parse operation I just declared:

localEntry= rootViewContoller.entry;

Upvotes: 0

Stephen Poletto
Stephen Poletto

Reputation: 3655

In all likelihood, rootViewController is nil. When you declare and synthesize a property, it only creates the getter/setter methods for you. It does not initialize the variable to anything.

Since objective-c allows you to message nil, you won't crash when you write:

NSString *localentry = rootViewController.entry;

Messaging nil just returns nil. So if rootViewController is nil, then localentry will be nil as well.

Make sure you're actually setting rootViewController for this class. For example,

ParseOperation *myOperation = [[ParseOperation alloc] init];
[myOperation setRootViewController:rootViewController];

Or, make sure you've established the outlet connection in Interface Builder. In any case, I'd suspect rootViewController is nil. (You can test this with NSLog statements).

Upvotes: 3

Dmacpro
Dmacpro

Reputation: 491

Are you sure, since it is an IBOutlet, that you connected to it in interface builder?

Upvotes: 2

Related Questions