How to retrieve a string from UITextField use it for a Parse query with PFQuery and then load to a Table View

StackOverflow https://stackoverflow.com/questions/23587443

  •  19-07-2023
  •  | 
  •  

I would like to create a basic search function without search bar display. I've created a UITextfield for the input text, a search button for the PFQuery and a table view where i want to display the results of the search. (User types a name to into the textfield, hit enter and if there is a match the results will appear in the table view.)

I tried to use this code for the query without any success. The xcode says "Unused variable 'user'" and the same with the searchResult. I don't understand why are these variables unused.

It's from the log:

A long-running Parse operation is being executed on the main thread. Break on warnParseOperationOnMainThread() to debug.

my .m file:

@interface TestViewController () 

@end

@implementation TestViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// mainArray = [query findObjects];
mainArray = [[NSArray alloc] initWithObjects:@"user", nil];

}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [mainArray count];
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"thisCell"];
cell.textLabel.text = [mainArray objectAtIndex:indexPath.row];
return cell;
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}


- (IBAction)searchButton:(id)sender {
  NSString *searchResult = [self.searchField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:@"searchResult"];
PFUser *user = (PFUser *)[query getFirstObject];
// NSArray *searchedItem = [query findObjects];

}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
@end

.h file:

@interface TestViewController :UIViewController<UITableViewDelegate,UITableViewDataSource>{

    IBOutlet UITableView *tableView;
    NSArray *mainArray;
    NSString *searchResult;
}

@property (weak, nonatomic) IBOutlet UITextField *searchField;
- (IBAction)searchButton:(id)sender;

@end
有帮助吗?

解决方案

You have no errors. "Unused" is a warning that means you're not using the "user" variable for anything. Sure, you're giving it a value, but you're not using that value afterwards. A simple call to NSLog displaying something will remove that warning:

NSLog(@"Username: %@", user[@"username"];

Also, as shim stated, the warning about a long-running Parse operation is because you use

[query getFirstObject];

which is run on the main thread. To get rid of that warning, you use one of the background functions, like

[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
  if (object) {
    PFUser *user = (PFUser *)object;
    NSLog(@"Username: %@", user[@"username"]);
  }
}];

UPDATE

Edit the function like this (no quotes for searchResult):

- (IBAction)searchButton:(id)sender {
  NSString *searchResult = [self.searchField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  PFQuery *query = [PFUser query];
  [query whereKey:@"username" equalTo:searchResult];
  PFUser *user = (PFUser *)[query getFirstObject];
  // NSArray *searchedItem = [query findObjects];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top