0

Trying to see my search results so I can properly format them, but I'm having trouble. I'm using the Yelp iOS API and it gives an example of what to call.

[self.client searchWithLocation:@"San Francisco, CA" completionHandler:^
    (YLPSearch *search, NSError *error) {
    // Perform any tasks you need to here
}];

Which seems simple enough. I'm coming from a JavaScript background so naturally I try a simple console.log-esque of the the callback, but it seems to be outputting memory location data, and not anything readable by me.

[self.client searchWithLocation:@"San Francisco, CA" 
              completionHandler:^ (YLPSearch *search, NSError *error) {
         NSLog(@"%@", search.businesses);
}];

// AppDelegate.h

#import <UIKit/UIKit.h>
#import <YelpAPI/YelpAPI.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
NS_ASSUME_NONNULL_BEGIN
@property (strong, nonatomic) UIWindow *window;

- (void)searchWithLocation:(NSString *)location
         completionHandler:(YLPSearchCompletionHandler)completionHandler;
NS_ASSUME_NONNULL_END
@end

// AppDelegate.m

#import "AppDelegate.h"

@interface AppDelegate ()

@property (nonatomic, strong) YLPClient *client;

@end

@implementation AppDelegate

-(void)searchWithLocation:(NSString *)location
        completionHandler:(YLPSearchCompletionHandler)completionHandler {
    NSLog(@"I did a thing");
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    [YLPClient authorizeWithAppId:@"jkdfhkdnfd"
                           secret:@"abcdefg123"
                completionHandler:^
     (YLPClient *client, NSError *error) {
         // Save your newly authorized client
         self.client = client;

        [self.client searchWithLocation:@"San Francisco, CA"
                      completionHandler:^
         (YLPSearch *search, NSError *error) {
             NSLog(@"%@", search.businesses);
        }];
     }];

    return YES;
}

Here's what it returns:

"<YLPBusiness: 0x6080000b5cc0>",
"<YLPBusiness: 0x6080000b5d20>",
"<YLPBusiness: 0x6080000b5d80>",
"<YLPBusiness: 0x6080000b5de0>",
"<YLPBusiness: 0x6080000b5e40>",
"<YLPBusiness: 0x6080000b5ea0>",
"<YLPBusiness: 0x6080000b5f00>",
"<YLPBusiness: 0x6080000b5f60>",
"<YLPBusiness: 0x6080000b5fc0>",
"<YLPBusiness: 0x6080000b6020>",
"<YLPBusiness: 0x6080000b6080>",
"<YLPBusiness: 0x6080000b60e0>",
"<YLPBusiness: 0x6080000b6140>",
"<YLPBusiness: 0x6080000b61a0>",
"<YLPBusiness: 0x6080000b6200>",
"<YLPBusiness: 0x6080000b6260>",
"<YLPBusiness: 0x6080000b62c0>",
"<YLPBusiness: 0x6080000b6320>",
"<YLPBusiness: 0x6080000b6380>",
"<YLPBusiness: 0x6080000b63e0>"
itsclarke
  • 8,622
  • 6
  • 33
  • 50

2 Answers2

1

Kind of like what the other person said, you're printing out the objects. The logs you're getting right now are an array of YLPBusiness objects.

You're logging this property here: https://github.com/Yelp/yelp-ios/blob/master/Classes/Response/YLPSearch.h#L17

And the businesses property is an array of YLPBusiness: https://github.com/Yelp/yelp-ios/blob/master/Classes/Response/YLPBusiness.h

So knowing that businesses is an array, try to iterate over it:

[self.client searchWithLocation:@"San Francisco, CA" 
              completionHandler:^ (YLPSearch *search, NSError *error) {
         for (YLPBusiness *business in search.businesses) {
             NSLog(@"%@", business.name)
             NSLog(@"%@", business.phone)
    }
}];

Here, I'm just logging the business name and phone, but you can log the many other properties of business in the YLPBusiness header file I linked above.

I didn't try it out because I don't have an account set up, but I think this should help

Gabriel Pires
  • 926
  • 9
  • 12
0

When you use the & it's because it's associated with a pointer to a pointer. Kind of confusing but not something you want for logging your return and just the error.

You can print properties of the YLPSearch custom object instead and it'll give you a value. But when it comes to custom objects you'll need to let Xcode know what it should print out by using the description method. Otherwise it doesn't know the specifics you want.

s.m.40
  • 1
  • Thanks for the info. I updated my question. I'm able to log out info, but it's nothing useful. – itsclarke Sep 01 '17 at 18:03
  • Yea, that's expected because in the `YLPBusiness` class you'll need a `description` method as well to let it know what you'd like it to print out. Then when you NSLOG it will log whatever you put in the function. These are nested custom objects. You can only NSLog foundation types. – s.m.40 Sep 01 '17 at 18:12