问题描述
好吧,这应该很容易,但是我仍然会在这里伤脑筋:
OK this should be an easy one but still im breaking my head here:
在我的根视图控制器中,我有一个 NSString
称为 entry,并且运行良好。我 NSLogged
它可以工作。
我有另一个名为 ParseOperation的类,其中有一个名为 localEntry的 NSString
,我试图将变量 entry发送给 ParseOperation来自 RootViewController,这是我的RootViewController代码:
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 NSString
called "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;
它只是不起作用。如果我在ParseOperation.m中 NSLog
,它将返回 null,但是如果我在RootViewController上执行它,它将返回正确的变量。是的,我确实导入了ParseOperation.h
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
这里是ParseOperation代码(仅使用localEntry的部分):
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;
}
}
}
谢谢!
推荐答案
要回答我自己的问题,我只需要通过将以下内容添加到parseOperation的标头中,以编程方式连接viewController和ParseOperation:
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;
以下是ParseOperation的m文件:
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;
这篇关于Xcode:将NSString从一个类传递到另一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!