In a view there is a text field and a picker view. The picker view items are loaded from a Parse query.
The text field text is always one of the picker view items.
The picker view selected item must be the same as the text field text. That is my code for the moment, but it throws an exception:
PFQuery *query = [PFQuery queryWithClassName:#"floors"];
[query whereKey:#"floor_restaurant" equalTo:self.restaurante.objectId];
[query orderByDescending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
_pickerData = objects;
for (int i = 0; i < [objects count]; i++)
{
if ([[objects objectAtIndex:i] isEqualToString: self.floor_name_text.text]){
[self.floor_picker selectRow:i inComponent:0 animated:YES];
break;
}
}
[self.floor_picker reloadAllComponents];
}
else {
NSLog(#"error");
}
}];
That is the exception:
2015-02-28 21:03:20.707 RestAppXXI[675:60b] -[PFObject isEqualToString:]: unrecognized selector sent to instance
I am new to Parse with iOS and any help is welcome.
EDITED :
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
PFObject *object = _pickerData[row];
return object[#"floor_name"];
}
Thanks to the comments of rmaddy and a little searching, I have resolved my issue, here is the final code that works:
[PFQuery *query = [PFQuery queryWithClassName:#"floors"];
[query whereKey:#"floor_restaurant" equalTo:self.restaurante.objectId];
[query orderByDescending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
_pickerData = objects;
for (int i = 0; i < [objects count]; i++)
{
NSString *textoactual = self.floor_name_text.text;
if ([[[objects objectAtIndex:i] objectForKey:#"floor_name" ] isEqualToString: textoactual]){
[self.floor_picker reloadAllComponents];
[self.floor_picker selectRow:i inComponent:0 animated:YES];
break;
}
}
}
else {
NSLog(#"error");
}
}];
Related
I got this warning and find no solution to fix it. In the code below I try to search the username from the parse.com.The error is located at menuItem.text line. Can someone please help me to fix this error .Thank you.
here is my code.
#interface FriendsScene()
#property(nonatomic) NSArray *user;
#end
-(void)didMoveToView:(SKView *)view {
PFQuery *query = [PFUser query];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[query whereKey:#"username" containsString:#"test"];
self.user = [[query findObjects]valueForKey:#"username"];
[self addNode];
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
}
-(void)addNode{
// add menu background
menuNode = [SKSpriteNode spriteNodeWithColor:[SKColor lightGrayColor] size:CGSizeMake(200, 100)];
menuNode.name = #"menuNode";
menuNode.position = CGPointMake(100, 0);
menuNode.zPosition = 10;
[self addChild:menuNode];
float yPos = -450;
for (int i = 0; i < 2; i++) {
SKLabelNode *menuItem = [SKLabelNode labelNodeWithFontNamed:#"HelveticaNeue"];
menuItem.name = [NSString stringWithFormat:#"menuItem-%i",i];
menuItem.text = [NSString stringWithFormat:#"%#",[self.user objectAtIndex:i]];
menuItem.fontSize = 20;
menuItem.fontColor = [SKColor redColor];
menuItem.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeCenter;
menuItem.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
menuItem.position = CGPointMake(0, yPos);
menuItem.zPosition = 25;
[menuNode addChild:menuItem];
yPos += 40;
}
}
To find a user with a particular user name, you just need one query on the PFUser class. Qualify that query with the name you wish to match. The result will be an array of objects that match:
PFQuery *query = [PFUser query];
[query whereKey:#"username" containsString:#"test"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
if (objects.count) {
self.user = objects[0];
} else {
// the query worked, but no users were found with that username
}
[self addNode];
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
I'm am fairly new to ios programming and to parse, so if i am not explaining something clearly ill be more than happy to try my best to expand on this.
In parse i have three tables User Activity and post the code i have now works well for a "following" relationship but does not for a "like" relationship, i want to take the object id from the Post table and the object id of the user that is liking the post from the User table, and insert them in to the Activity Table. Any help will be appreciated, Thank you in advanced.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == self.objects.count && self.paginationEnabled) {
[self loadNextPage];
}
}
- (PFQuery *)queryForTable
{
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query includeKey:#"User"];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
[query whereKey:#"filter" hasPrefix:#"90"];
[query orderByDescending:#"createdAt"];
return query;
}
- (void)likeButton:(LikeButton *)button didTapWithSectionIndex:(NSInteger)index {
PFObject *post = [self.objects objectAtIndex:index];
[post fetchIfNeeded];
PFUser *user = post[#"User"];
if (!button.selected) {
[self likePost:user];
}
else {
[self unlikePost:user];
}
[self.tableView reloadData];
}
- (void)likePost:(PFUser *)user {
if (![user.objectId isEqualToString:[PFUser currentUser].objectId]) {
[self.likeArray addObject:user.objectId];
PFObject *likeActivity = [PFObject objectWithClassName:#"Activity"];
likeActivity[#"fromUser"] = [PFUser currentUser];
likeActivity[#"toPost"] = user;
likeActivity[#"type"] = #"Like";
[likeActivity saveEventually];
}
}
- (void)unlikePost:(PFUser *)user {
[self.likeArray removeObject:user.objectId];
PFQuery *query = [PFQuery queryWithClassName:#"Activity"];
[query whereKey:#"fromUser" equalTo:[PFUser currentUser]];
[query whereKey:#"toPost" equalTo:user];
[query whereKey:#"type" equalTo:#"like"];
[query findObjectsInBackgroundWithBlock:^(NSArray *likeActivities, NSError *error) {
if (!error) {
for (PFObject *likeActivity in likeActivities) {
[likeActivity deleteEventually];
}
}
}];
}
OK, first off I would mention that you should be very careful with case-sensitivity. String matches in Parse are case-sensitive so if you create it with type:#"Like" and then try to find it with whereKey:#"type" equalTo:#"like" you won't get a match!
Now, as for your code, you want to link the current user to the selected post right?
If so here's a sample of what you should do instead:
- (void)likeButton:(LikeButton *)button didTapWithSectionIndex:(NSInteger)index {
PFObject *post = [self.objects objectAtIndex:index];
if (!button.selected) {
[self likePost:post];
}
else {
[self unlikePost:post];
}
- (void)likePost:(PFObject *)post {
[self.likeArray addObject:user.objectId];
PFObject *likeActivity = [PFObject objectWithClassName:#"Activity"];
likeActivity[#"fromUser"] = [PFUser currentUser];
likeActivity[#"toPost"] = post;
likeActivity[#"type"] = #"like";
[likeActivity saveEventually];
}
Looks like your code was copy-pasted from your "follow" logic but not updated to reflect the fact that you're now doing a "like", so I've fixed that for you. You'll need to make similar changes to your unlikePost: method too.
I have a parse iOS app where I need to search the names of a couple thousand users (~3,000). I am trying to modify my search code so that I can do this but I need help. Right now my code for search looks like this:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == self.tableView) {
return self.objects.count;
} else {
return self.searchResults.count;
}
}
-(void)filterResults:(NSString *)searchTerm :(int)limit :(int)skip {
[self.searchResults removeAllObjects];
PFQuery *query = [PFQuery queryWithClassName:#"_User"];
[query whereKey:#"isTeacher" equalTo:#"False"];
[query setLimit: limit];
[query setSkip: skip];
[[[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (objects.count == limit) {
[self performTeacherQueryWithLimit:limit andSkip:skip+limit];
}
else{
NSArray *results = [NSArray arrayWithArray:objects];
NSLog(#"%#", results);
NSLog(#"%lu", (unsigned long)results.count);
NSLog(#"results^");
[self.searchResults addObjectsFromArray:results];
NSPredicate *searchPredicate =
[NSPredicate predicateWithFormat:#"SELF.name contains[c] %#",searchTerm];
_searchResults = [NSMutableArray arrayWithArray:[results filteredArrayUsingPredicate:searchPredicate]];
[self.searchDisplayController.searchResultsTableView reloadData];
NSLog(#"%#", _searchResults);
NSLog(#"%lu", (unsigned long)_searchResults.count);
NSLog(#"search results^");
}
}];
]]
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[self filterResults:searchString];
return YES;
}
This code will not work because I call the filterResults at the bottom without all the correct parameters but that is because I got halfway through and now I am stuck. I know I need to use the setSkip but I'm not sure how to make that work for my searching. Any help would be awesome! Thanks!
I do this sort of thing with a method that handles the query and its results together, like this:
- (void)runQuery:(PFQuery *)query filling:(NSMutableArray *)array completion:(void (^)(BOOL))completion {
query.skip = array.count;
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
[array addObjectsFromArray:objects];
if (objects.count < query.limit) {
return completion(YES);
} else {
[self runQuery:query filling:array completion:completion];
}
} else {
return completion(NO);
}
}];
}
Use it like this:
PFQuery *query = [PFQuery queryWithClassName:#"MyClass"];
// setup query
query.limit = // set this to a reasonable size
// the given method will do ceil(N / limit) finds, where N is the number
// of rows that satisfy the query
NSMutableArray *array = [#[] mutableCopy];
[self runQuery:query filling:array completion:^(BOOL success) {
NSLog(#"%#", array);
// you would do your local search and set search results here
}];
A more functional version would call a progress block in between queries. This would allow you to continuously update results. For that, just add a progress block parameter and call it right after [array addObjectsFromArray:objects];.
I don't understand how this is possible or why this is happening. Can anyone give me an idea?
I added more code to help you figure it out, however, i'm not sure how the extra code will help.
NSMutableArray *messages = [[NSMutableArray alloc] init];
PFUser *currentUser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:#"message"];
[query whereKey:#"receiver" equalTo:currentUser.objectId];
[query orderByDescending:#"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error && objects !=NULL) {
NSMutableArray *listOfLastMsgs = [[NSMutableArray alloc] init];
for (PFObject *object in objects) {
Boolean *tester = false;
for(int i = 0;i<listOfLastMsgs.count;i++){
if([[object objectForKey:#"sender"] isEqualToString:[[listOfLastMsgs objectAtIndex:i] objectForKey:#"sender"]]){
tester = true;
}
}
if(!tester){
[listOfLastMsgs addObject:object];
}
}
for(int i = 0;i<listOfLastMsgs.count;i++){
NSString *s = [[listOfLastMsgs objectAtIndex:i] objectForKey:#"message"];
if(s.length > 80){
s = [s substringToIndex:80];
}
[messages addObject:s];
}
NSLog(#"in did load %lu", (unsigned long)messages.count);
[self.tableView reloadData];
} else {
// Log details of the failure
NSLog(#"Error: %# %#", error, [error userInfo]);
}
}];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(#"in number of rows %lu", (unsigned long)messages.count);
return messages.count;
}
Here is my output log
2014-07-25 18:15:32.980 myapp[5696:60b] in number of rows 0
2014-07-25 18:15:35.990 myapp[5696:60b] in did load 2
2014-07-25 18:15:35.991 myapp[5696:60b] in number of rows 0
Assuming that big hunk of code is in your viewDidLoad the problem is simple - you have a local messages variable that you setup in viewDidLoad and your numberOfRowsInSection is referencing an uninitialized messages instance variable.
Assuming you really do have an ivar named messages, in viewDidLoad, change:
NSMutableArray *messages = [[NSMutableArray alloc] init];
to:
messages = [[NSMutableArray alloc] init];
I would want to to use two carousel using the icarousel to display images from parse. This is my the code when retrieving images from the document folder.
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
if (carousel == carousel1)
{
return [items1 count];
PFQuery *query = [PFQuery queryWithClassName:#"TopsC"];
PFUser *user = [PFUser currentUser];
[query whereKey:#"user" equalTo:user];
[query orderByAscending:#"createdAt"];
}
else
{
return [items2 count];
PFQuery *query = [PFQuery queryWithClassName:#"BottomC"];
PFUser *user = [PFUser currentUser];
[query whereKey:#"user" equalTo:user];
[query orderByAscending:#"createdAt"];
}
}
ITEMS
- (void)awakeFromNib
{
NSLog(#"ooooo");
//set up data sources
self.items1 = [NSMutableArray array];
for (int i = 0; i < 100; i++)
{
[items1 addObject:[NSNumber numberWithInt:i]];
}
self.items2 = [NSMutableArray array];
for (int i = 65; i < 65 + 58; i++)
{
[items2 addObject:[NSString stringWithFormat:#"%c", i]];
}
}
However I don't nothing is displayed for the carousel. Why is it not importing photo from parse?
First; the query code never gets run, as you start with returning [items1 count];
Anything included after the return statement is ignored.
Second; There is nothing in the code you provided that return anything from Parse. Your data sources (items1/items2) get populated with numbers and strings in your awakeFromNib, but there is no contact with parse in this code. Except the queries for Topsc and BottomC, which is never executed. And even if they DID get executed, you don't trigger the queries. You just set them up.
So, to answer your question: you don't query parse for any images, and that's why the carousels don't get any images.