Formatting String in a Textview from data from an Array in Parse

Asked

Viewed 173 times

0

I’m making an application to register 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 student’s name and it will appear in a Textview, after clicking the search button.

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

My search method, the search button action, is as follows:

 - (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 is that mine textView always displays the results with the keys and some codes. I believe I am not formatting properly.

The result appears like this:

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

Could I get these keys and codes? I don’t know if stringWithFormat is the best way to display.

2 answers

0

You don’t have to loop through the elements of the arrays. All NSArray has the 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 in en".

0


I don’t 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 am going to suggest has not been tested ok? I believe that it 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 properties values firstName and lastName, you can also use the method objectForKey: who does the same thing.

Well I hope I helped, good luck!

  • Ok, my answer ta means "thigh" because I forgot that this is a repetition block, so you can use a Nsmutablestring to give the "append" of the names before assigning them to your text field. If you need a hand with that, just ask ;)

  • Nice of you to help. In the case of the mutable string, you can instantiate a variable of type Nsmutablestring and use the instance method "appendFormat" in the repetition block to scan all items within "Objects". Then just assign it to the text in your text view.

  • One thing that is happening now, is that whenever I click the search field it returns me the last registered, and never what I typed in the field. Would it have something to do with Dispatch, or the loop? I will try to make the appendFormat.

  • It makes sense, because in the snippet I sent you, it will always keep the last name and surname of the last record (my fault), when you do with appendFormat, you will have a string with all names and surnames merged.

  • hmm cool. I tried to make the appendFormat, but I’m really not getting it. I tried something like this: Nsmutablestring* searchAlunos = [[Nsmutablestring alloc] init]; and then self.textViewResgate.text = [searchAlunos appendFormat:@"firstName"]; I think the typing syntax is confusing me. Does the topic let you edit your answer up there in the code? I think I’ll get a better view. Thanks again Jan.

  • I think I got it. What happened was this, Parse’s method that I was using wasn’t doing the Query the right way. I picked one of the Keys to type in the search field, sounding the same as the typed one. So: [query whereKey:@"firstName" equalTo:self.searchTextField.text];Then I did what you said inside findobjectsinBackgroundWithBlock and Dispatch. Nsmutablestring* searchSilver = [[Nsmutablestring alloc] init]; and self.textViewResgate.text = [searchSilver stringByAppendingFormat:@"First name: %@",Object[@"firstName"],Object[@"lastname"]];

  • Thank you very much Jan. Needing too, just talk.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.