2011-03-30 40 views
1

确定这应该是容易的,但仍然我在这里打破了我的头:的Xcode:合格的NSString从一类到另一个问题

在我的根视图控制器我有一个NSString名为“项”,并可以正常使用。我NSLogged它和它的作品。 我有一个名为'ParseOperation'另一个类,并在其中我有一个NSString称为“localEntry”和IM试图发送到“ParseOperation”,从“RootViewController的”变量“条目”这是我的RootViewController的代码:

RootViewController.m 

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

它只是不起作用。如果我在我的ParseOperation.m中返回“NSLog”,但是如果我在我的RootViewController上执行该操作,它将返回正确的变量。是的,我也进口ParseOperation.h

这里是ParseOperation代码(仅使用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; 
     } 
    } 
} 

谢谢!

回答

0

回答我的问题,我不得不到ViewController和ParseOperation加入编程连接下面我在parseOperation头:

@class RootViewController; 

RootViewController *rootViewController; 

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

而且在ParseOperation的M档如下:

#import "RootViewController.h" 

rootViewController = [[RootViewController alloc]init]; 

之后,在分析操作我只是宣称:

localEntry= rootViewContoller.entry; 
4

很可能,rootViewController是零。当你声明和合成一个属性时,它只会为你创建getter/setter方法。它不会将变量初始化为任何内容。

由于Objective-C的让你的消息零,你会不会当你写崩溃:

NSString *localentry = rootViewController.entry; 

消息无直接返回零。所以如果rootViewController是零,那么localentry也是零。

确保你实际上为这个类设置了rootViewController。例如,

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

或者,确保您已在Interface Builder中建立了插座连接。无论如何,我怀疑rootViewController是零。 (你可以用NSLog语句来测试这个)。

+0

谢谢我以不同的方式开始执行tho ...而不是我声明的所有ParseOperation: 'ParseOperation * parseOperation = [[ParseOperation alloc] init];' 并发送变量,就像'parseOperation.localEntry = entry' 但仍然是相同的结果 – Ponchotg 2011-03-30 23:58:45

+0

@Ponchotg:parseOperation.localEntry = entry是调用setter的语法简写。它相当于[parseOperation setLocalEntry:entry]。在Objective-C 2.0中引入的点语法只是为了方便而隐藏了这个方法调用,但实际上它调用了setter。 – 2011-03-31 00:08:39

+0

我发了一些更多的代码,希望你能帮助和谢谢! – Ponchotg 2011-03-31 01:40:21

2

您确定,因为它是一个IBOutlet,您在界面构建器中连接到它吗?

+0

我做到了!但无论如何,我开始以不同的方式... ...而不是我声明的ParseOperation: 'ParseOperation * parseOperation = [[ParseOperation alloc] init];' 并发送变量就像'parseOperation.localEntry = entry' 但仍然是相同的结果 – Ponchotg 2011-03-30 23:56:43

+0

好吧在你分配alloc之后立即设置一个断点,初始化这个对象并确保它由于某种原因不返回null,并让我知道结果。 – Dmacpro 2011-03-31 00:16:31

+0

谢谢@Dmacpro回来了(我想):“parseOperation =(ParseOperation *)0x4b8ab90” – Ponchotg 2011-03-31 00:28:21