Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6598681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:23:02+00:00 2026-05-25T18:23:02+00:00

I have the following code,how can i reverse the messages Array ,see bellow: #import

  • 0

I have the following code,how can i reverse the messages Array,see bellow:

#import "newsViewController.h"
#import "DetailViewController.h"

@implementation newsViewController

@synthesize messageList;

//##############################################################################################################################
//#################                           CUSTOM VIEW INITALIZATION                                      #################//
//##############################################################################################################################
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        lastId = 0;
        chatParser = NULL;
    }
    return self;
}

//##############################################################################################################################
//#################                           DEALLOC - MEMORY RELEASE                                       #################//
//##############################################################################################################################
-(void)dealloc {
    [messageList release];
    [super dealloc];
}

//##############################################################################################################################
//#################                           DISPLAY PHP FILE INTEGRATION                                   #################//
//##############################################################################################################################
-(void)getNewMessages {

    NSString *url = [NSString stringWithFormat:@"http://localhost/sportApp/messages.php?past=%ld&t=%ld",lastId, time(0) ];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"GET"];

    NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];  

    if (conn){  
        receivedData = [[NSMutableData data] retain];  
    }else{}  
}

//##############################################################################################################################
//#################                                FETCHING PRAGMAS                                          #################//
//##############################################################################################################################
-(void)timerCallback {
    [timer release];
    [self getNewMessages];
}

//##############################################################################################################################
//#################                             CONNECTION PRAGMAS                                           #################//
//##############################################################################################################################
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [receivedData setLength:0];
}  
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
}  
-(void)connectionDidFinishLoading:(NSURLConnection *)connection  {  

    if (chatParser)
        [chatParser release];

    if (messages == nil)

        messages = [[NSMutableArray alloc] init];

    chatParser = [[NSXMLParser alloc] initWithData:receivedData];
    [chatParser setDelegate:self];
    [chatParser parse];

    [receivedData release];  
    [messageList reloadData];

    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector: @selector(timerCallback)]];
    //[invocation setTarget:self];
    [invocation setSelector:@selector(timerCallback)];
    timer = [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO];
}  

//##############################################################################################################################
//#################                         PARSING THE MESSAGE XML FILE LIST                                #################//
//##############################################################################################################################
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict {

    if ( [elementName isEqualToString:@"message"] ) {

        msgAdded = [[attributeDict objectForKey:@"added"] retain];
        msgId = [[attributeDict objectForKey:@"id"] intValue];

        msgUser   = [[NSMutableString alloc] init];
        msgText   = [[NSMutableString alloc] init];
        msgText2  = [[NSMutableString alloc] init];
        msgImage  = [[NSMutableString alloc] init];
        msgVideo  = [[NSMutableString alloc] init];

        inUser   = NO;
        inText   = NO;
        inText2  = NO;
        inImage  = NO;
        inVideo  = NO;
    }
    if ( [elementName isEqualToString:@"user"] )     { inUser = YES;  }
    if ( [elementName isEqualToString:@"text"] )     { inText = YES;  }
    if ( [elementName isEqualToString:@"subtext"] )  { inText2 = YES; }
    if ( [elementName isEqualToString:@"image"] )    { inImage = YES; }
    if ( [elementName isEqualToString:@"ytvideo"] )  { inVideo = YES; }


}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {

    if ( inUser )  { [msgUser appendString:string]; }
    if ( inText )  { [msgText appendString:string]; }
    if ( inText2 )  { [msgText2 appendString:string]; }
    if ( inImage ) { [msgImage appendString:string];}
    if ( inVideo ) { [msgVideo appendString:string];}

}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

    if ( [elementName isEqualToString:@"message"] ) {

        [messages addObject:[NSDictionary dictionaryWithObjectsAndKeys:msgAdded,@"added",msgUser,@"user",msgText,@"text",msgText2,@"subtext",msgImage,@"image",msgVideo,@"ytvideo",nil]];

        [[messages reverseObjectEnumerator] allObjects];

        lastId = msgId;

        [msgAdded release];
        [msgUser release];
        [msgText release];
        [msgText2 release];
        [msgImage release];
        [msgVideo release];

    }

    if ( [elementName isEqualToString:@"user"]   ) { inUser = NO;}
    if ( [elementName isEqualToString:@"text"]   ) { inText = NO;}
    if ( [elementName isEqualToString:@"subtext"]   ) { inText2 = NO;}
    if ( [elementName isEqualToString:@"image"]  ) { inImage = NO;}
    if ( [elementName isEqualToString:@"ytvideo"]) { inVideo = NO;}
}

//##############################################################################################################################
//#################                         PARSING FINISHED - START DISPLAYING                              #################//
//##############################################################################################################################
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
-(NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section {
    return ( messages == nil ) ? 0 : [messages count];
}
-(UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:@"newsCustomCell"];
    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"newsCustomCell" owner:self options:nil];
        cell = (UITableViewCell *)[nib objectAtIndex:0];
    }

    NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
    UILabel *timeDate = (UILabel *)[cell viewWithTag:1];
    timeDate.text = [itemAtIndex objectForKey:@"added"];
    UILabel *userL = (UILabel *)[cell viewWithTag:2];
    userL.text = [itemAtIndex objectForKey:@"user"];
    UILabel *textL = (UILabel *)[cell viewWithTag:3];
    textL.text = [itemAtIndex objectForKey:@"text"];
    UILabel *textL2 = (UILabel *)[cell viewWithTag:4];
    textL2.text = [itemAtIndex objectForKey:@"subtext"];
    UILabel *imageL = (UILabel *)[cell viewWithTag:5];
    imageL.text = [itemAtIndex objectForKey:@"image"];
    UILabel *videoL = (UILabel *)[cell viewWithTag:6];
    videoL.text = [itemAtIndex objectForKey:@"ytvideo"];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:[NSBundle mainBundle]];

    NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];

    NSString *selectTime = [itemAtIndex objectForKey:@"added"];

    NSString *selectUser = [itemAtIndex objectForKey:@"user"];

    NSString *selectMessage = [itemAtIndex objectForKey:@"text"];

    NSString *selectMessage2 = [itemAtIndex objectForKey:@"subtext"];

    NSString *selectImage = [itemAtIndex objectForKey:@"image"];

    NSString *selectVideo = [itemAtIndex objectForKey:@"ytvideo"];

    dvController.selectedTime = selectTime;
    dvController.selectedUser = selectUser;
    dvController.selectedImage = selectImage;
    dvController.selectedMessage = selectMessage;
    dvController.selectedMessage2 = selectMessage2;
    dvController.selectedVideo = selectVideo;


    [self.navigationController pushViewController:dvController animated:YES];
    [dvController release];
    dvController = nil;
}

//##############################################################################################################################
//#################                         PARSING FINISHED - START DISPLAYING                              #################//
//##############################################################################################################################
-(void)viewDidLoad {    
    messageList.dataSource = self;
    messageList.delegate = self;

    //@@@@@@@@TRY
    [[messages reverseObjectEnumerator] allObjects];

    [self getNewMessages];
    [super viewDidLoad];

}



@end

I’ve already searched for reverse a NSArray but all methods didn’t work for me,help please!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:23:03+00:00Added an answer on May 25, 2026 at 6:23 pm

    You need something like:

    NSMutableArray* reversedMessages = [NSMutableArray arrayWithCapacity:[messages count]];
    NSEnumerator*   reverseEnumerator = [messages reverseObjectEnumerator];
    for (id object in reverseEnumerator)
    {
        [reversedMessages addObject:Object];
    }
    

    You could then assign reversedMessages to messages or do whatever else you want with it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written the following code: As you can see there is a for
I have the following code and I can't understand what does it mean: var1
I have the following code to do this, but how can I do it
Please can someone help? I have the following code which uploads a file to
I have a simple 3D cube that I can rotate using the following code:
According to what I have found so far, I can use the following code:
Following on from this question I now have code that can attach to a
We have similar code to the following in one of our projects. Can anyone
I have the following code implementation of Breadth-First search. trait State{ def successors:Seq[State] def
I have the following code and I can't seem to get it to work.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.