String Formatting in a TextView from Data from an Array in Parse

0

I am making an application for registering students from a school using TabBar, and one of the Tabs is the Query Tab.

In this tab, I have a search field, where the user must enter the name of the student and it will appear in a TextView, after clicking the search button.

I'm using the Parse server, which provides me with "Query" methods for the data, including already providing an Array where objects are stored.

My search method, the action of the fetch button, looks like this:

 - (IBAction)buscarAlunos:(id)sender {

 // Busca
    PFQuery *query = [PFQuery queryWithClassName:@"Aluno"];

 // Busca com as Keys abaixo    
    [query selectKeys:@[@"firstName",@"lastName",@"telephone,classDate"]];

 // Método fornecido pelo Parse
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

     // Método para efetuar a busca no Array objects com os dados do campo
        [query whereKey:self.buscarTextField.text containsAllObjectsInArray:objects];

    // Array para guardar os itens da busca  
        NSMutableArray* busca = [[NSMutableArray alloc]init];

       [busca addObject:[NSString stringWithFormat:@"%@",[objects componentsJoinedByString:@"\n"]]];

   // Exibir no textView
       self.textViewResgate.text = [busca componentsJoinedByString:@"\n"];
    }];
}

The problem that occurs is that my textView always displays the results with the keys and some codes. I believe I'm not formatting correctly.

The result looks like this:

<Aluno: FA49wkjYMA:(null)>{
firstName=Jose
lastName=da Silva
}

Would I have to get those keys and codes? I do not know if stringWithFormat is the best way to display.

    
asked by anonymous 08.03.2014 / 23:24

2 answers

0

I do not know exactly how this Parse method works (I've never actually used Parse), but looking at your code, as you said, I also think it's a matter of formatting. What I will suggest was not tested ok? I believe this can work the way you want, come on!

- (IBAction)buscarAlunos:(id)sender {

 // Busca
    PFQuery *query = [PFQuery queryWithClassName:@"Aluno"];

 // Busca com as Keys abaixo    
    [query selectKeys:@[@"firstName",@"lastName",@"telephone,classDate"]];

 // Método fornecido pelo Parse
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

     // Método para efetuar a busca no Array objects com os dados do campo
        [query whereKey:self.buscarTextField.text containsAllObjectsInArray:objects];

   // Exibir no textView (coloquei dentro de um gdc direcionando para a main thread, por que como esse bloco esta em background)
      dispatch_async(dispatch_get_main_queue(),{

        for(NSDictionary *object in objects)
          self.textViewResgate.text = [NSString stringWithFormat:@"%@ %@", object[@"firstName"], object[@"lastName"]];

      });

    }];
}

Note that I used bracket notation ( [] ) to get the values of the firstName and lastName properties, you can also use the objectForKey: method that does the same thing.

Well I hope to have helped, good luck!

    
09.03.2014 / 19:23
0

You do not have to loop through the elements of the arrays. All NSArray has method componentsJoinedByString , where you pass a separator character, say a comma, and it mounts the description string.

NSArray *words = [NSArray arrayWithObjects:@"Olá", @"Stackoverflow", @"em", @"pt-BR", nil];
NSLog(@"%@",[word componentsJoinedByString:@" "]);

For example, the result of the two lines above will be "Hello Stackoverflow en pt-BR".

    
15.04.2014 / 08:01