在另一个类中显示NSMutableArray的内容

时间:2012-10-25 19:14:31

标签: xml class parsing

我已将tweets加载到NSMutableArray中,我想在另一个类中,textview或类似内容中显示最新的内容。

Tweet.h:

#import <Foundation/Foundation.h>

@interface Tweet : NSObject


@property (strong, nonatomic)NSString *content;
@property (strong, nonatomic)NSString *dateCreated;



@end

Tweet.m:

#import "Tweet.h"

@implementation Tweet

@synthesize content = _content;
@synthesize dateCreated = _dateCreated;


@end

XMLParser.h:

#import "Foundation/Foundation.h"
#import "Tweet.h"

@interface XMLParser : NSObject <NSXMLParserDelegate>

@property (strong, readonly) NSMutableArray *tweets;

-(id) loadXMLByURL:(NSString *)urlString;

@end

XMLParser.m:

#import "Foundation/Foundation.h"
#import "XMLParser.h"


@implementation XMLParser
@synthesize tweets = _tweets;

NSMutableString *currentNodeContent;
NSXMLParser *parser;
Tweet *currentTweet;
bool isStatus;

bool isdone = true;



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

- (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:@"status"])
    {
    currentTweet = [Tweet alloc];
    isStatus = YES;
    }
    if ([elementName isEqualToString:@"user"])
    {
        isStatus = NO;
    }
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if (isStatus) {
        if ([elementName isEqualToString:@"text"])
        {
            currentTweet.content = currentNodeContent;
        }
        if ([elementName isEqualToString:@"created_at"])
        {
            currentTweet.dateCreated = currentNodeContent;
        }

    }
    if ([elementName isEqualToString:@"status"])
    {
        [self.tweets addObject:currentTweet];
        currentTweet = nil;
        currentNodeContent = nil;
    }
}

@end

ProgramTeknik.h:

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

@interface ProgramTeknik : UIViewController
@property (weak, nonatomic) IBOutlet UITextView *teknikNews;

@end

ProgramTeknik.m:

#import "ProgramTeknik.h"
#import "ViewController.h"
#import "XMLParser.h"
#import "Tweet.h"


@interface ProgramTeknik ()

@end

@implementation ProgramTeknik


XMLParser *xmlParser; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{


    [super viewDidLoad];
    // Do any additional setup after loading the view.

    xmlParser = [[XMLParser alloc] loadXMLByURL:@"http://api.twitter.com/1/statuses/user_timeline/baristashopen.xml"];

_teknikNews.text = @"test";

}

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

@end

我想在ProgramTeknik课程中显示推文,你们有什么想法吗? 非常感谢!

0 个答案:

没有答案