无法访问NSObject类iOS

时间:2013-12-30 22:17:34

标签: ios objective-c xml class

我有一个名为XMLParser的类,我希望能够从ViewController类访问此类中的方法,但由于某种原因,它不允许我这样做。 有关如何在viewDidLoad中加载它的任何帮助吗?

XMLParser.h

#import <Foundation/Foundation.h>
#import "ViewController.h"

@interface XMLParser : NSObject

{
    bool isStatus;
    XMLParser *currentProfile;
    XMLParser *xmlParser;
    NSXMLParser *parser;
    NSMutableString *currentNodeContent;
    NSMutableArray *profile;
    NSString *firstName;
}

- (void)loadXMLByURL:(NSString *)urlString;
- (void)loadXML;

@end

XMLParser.m

#import "XMLParser.h"

@implementation XMLParser

-(void)loadXMLByURL:(NSString *)urlString
{
    profile = [[NSMutableArray alloc] init];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    parser = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if([elementName isEqualToString:@"firstname"])
    {
        currentProfile = [XMLParser alloc];
        isStatus = YES;
    }
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if([elementName isEqualToString:@"firstname"])
    {
        currentProfile->firstName = currentNodeContent;
        NSLog(@"%@",currentProfile->firstName);
        [profile addObject:currentProfile];
    }
}

-(void)loadXML
{
    [self loadXMLByURL:@"http://dierenpensionlindehof.nl/profiles1.xml"];
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "XMLParser.h"

@interface ViewController : UIViewController
{

}

@end

ViewController.m

#import "ViewController.h"
#import "XMLParser.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    XMLParser = [[XMLParser alloc]init]; //======This doesn't seem to work?
    [XMLParser loadXML];//=======================This doesn't seem to work?
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

在标签中显示分配数据的代码

- (IBAction)showProfilesButton:(id)sender
{
    XMLParser* parser = [[XMLParser alloc]init];
    [parser loadXML];


    NSMutableString *str = [[NSMutableString alloc] init];

    for(XMLParser *obj in self.profile)
    {
        [str appendFormat:@"\n %@", obj->firstName];
    }

    CGSize expectedSize = [str sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]] constrainedToSize:CGSizeMake(520, 1000) lineBreakMode:NSLineBreakByWordWrapping];

    CGRect newFrame = CGRectMake(0, 0, expectedSize.width, expectedSize.height);
    [profilesLabel setFrame:newFrame];
    [profilesLabel setText:str];

}

1 个答案:

答案 0 :(得分:3)

该行

XMLParser = [[XMLParser alloc]init];

不分配变量,在语法上不正确。你可能想要更像

的东西
XMLParser* parser = [[XMLParser alloc]init];

这会将已分配和初始化的实例正确分配给局部变量“parser”。

要在之后使用它,您可以致电

[parser loadXML];