Twitter Feed没有显示完整的推文

时间:2013-04-03 04:55:51

标签: ios xml xcode twitter

所以我正在创建一个应用程序,其中一部分在表格视图中显示用户推文。然而,有一些东西被一些推文损坏,例如它只显示单个字符,如(“)或表情符号字符。例如,如果推文说:

RT @jakemillermusic: Everyone upload your pics that you took today during the ustream and caption it "follow @jakemillermusic #jakemiller"

与NSLog一起显示时打印:


2013-04-03 00:34:30.476 ParsingXMLTutorial [3308:c07] RT @jakemillermusic:每个人都上传你今天在ustream中拍摄的照片及其标题

2013-04-03 00:34:30.476 ParsingXMLTutorial [3308:c07]“

2013-04-03 00:34:30.477 ParsingXMLTutorial [3308:c07]关注@jakemillermusic #jakemiller

2013-04-03 00:34:30.478 ParsingXMLTutorial [3308:c07]“


以下是我用来获取XML格式的网址:

http://api.twitter.com/1/statuses/user_timeline/LexxiSaal.xml?include_entities=true&include_rts=true&screen_name=twitterapi&trim_user=false&contributor_details=true&count=50


以下是分类代码:

-(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
{
    //string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];           // space
    string = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];    // newline
    string = [string stringByReplacingOccurrencesOfString:@"\t" withString:@""];


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:@"created_at"]) 
            {
                currentTweet.dateCreated = currentNodeContent;
            }
            if ([elementname isEqualToString:@"text"]) 
            {
                currentTweet.content = currentNodeContent;
            }
        }
    if ([elementname isEqualToString:@"status"]) 
    {
        [self.tweets addObject:currentTweet];
        currentTweet = nil;
        currentNodeContent = nil;
    }
}

@end

1 个答案:

答案 0 :(得分:0)

我建议稍作修改

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

     if(currentNodeContent == nil){
         currentNodeContent = [[NSMutableString alloc] initWithString:value];
     }else
         [currentNodeContent appendString:value];
}

并在 didEnd

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:    (NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if (isStatus) 
    {
        if ([elementname isEqualToString:@"created_at"]) 
        {
            currentTweet.dateCreated = currentNodeContent;
        }
        if ([elementname isEqualToString:@"text"]) 
        {
            currentTweet.content = currentNodeContent;
        }
    }
    if ([elementname isEqualToString:@"status"]) 
    {
        [self.tweets addObject:currentTweet];
        currentTweet = nil;
        //currentNodeContent = nil; REMOVED
     }

    currentNodeContent = nil; // PUT OUTSIDE
}

问题是你必须在方法foundCharacters中附加字符串,因为解析器不会立即返回标记之间的所有字符串。

最后你应该在全局的didEnd方法中使 currentNodeContent 为nil,因为你也将它用于其他文本。

希望以上有所帮助..