1

ツイートを NSMutableArray にロードしました。最新のコンテンツを別のクラス、テキストビューなどに表示したいと思います。

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クラスでツイートを見せたいのですが、何かアイデアはありますか? どうもありがとう!

4

0 に答える 0