thepaperboy
thepaperboy

Reputation: 676

XML-RPC request with struct in iOS

I'm trying to use xmlrpc with wordpress to get post of a specific custom type (called a collection).

The Wordpress API doc states:

wp.getPosts

Parameters:
int blog_id
string username
string password
struct filter: Optional.
   string post_type
   string post_status
   int number
   int offset
   string orderby
   string order
array fields: Optional.

My problem is in forming a struct with strings in objc:

I am wanting to do something like this:

// in .h

typedef struct{
    string post_type;
    string post_status;
    int number;
    int offset;
    string orderby;
    string order;
} wp_filter;

// in .m

wp_filter filter = {@"collection", @"", ... , ... ,@"",@""};
NSArray *fieldsArray = [NSArray arrayWithObjects:@"post_title", nil];
NSArray *postParams = [NSArray arrayWithObjects:@"0", username, password, filter, fieldsArray, nil];
XMLRPCRequest *reqCollections =[[XMLRPCRequest alloc] initWithURL:[NSURL URLWithString:server]];

[reqCollections setMethod:@"wp.getPosts" withParameters:postParams];

XMLRPCResponse *customPostResponse = [XMLRPCConnection sendSynchronousXMLRPCRequest:reqCollections error:nil];

if ([[customPostResponse object] isKindOfClass:[NSArray class]]){
    NSArray *collections = [NSArray arrayWithArray:[customPostResponse object]];
    NSLog(@"number of collections %i",[collections count]);
    for (int i = 0; i < [collections count]; i++) {
        NSLog(@"%@", [[collections objectAtIndex:i] description] );
    }
}
else {
    NSLog(@"response description %@",[[customPostResponse object ] description]);
}

Upvotes: 0

Views: 1161

Answers (1)

AliSoftware
AliSoftware

Reputation: 32681

I don't know about the XML-RPC WordPress API, but structs are C constructs but not Cocoa NSObjects, so you cannot embed them in an NSArray.

So the [NSArray arrayWithObjects:..., filter, ..., nil] line is invalid, because filter is a wp_filter struct.

I think that the "struct" that is mentioned in the API documentation is more some pseudo-code to explain the data structure / organization. Obviously you need to "translate" that concept into Cocoa objects.

What you might try is to convert your C struct into either an NSArray and pass the parameters in the same order as the API expect, or more likely to convert your struct into an NSDictionary, whose keys are the name of the structs fields, and values are obviously your struct's field values.

Moreover, to encapsulate integers into Cocoa objects, you should use the NSNumber class instead of converting your integers into NSStrings.


So this sould give something like this:

NSDictionary* filter = [NSDictionary dictionaryWithObjectsAndKeys:
    @"collection", @"post_type",
    @"", @"post_status",
    [NSNumber numberWithInt:number], @"number",
    [NSNumber numberWithInt:offset], @"offset",
    @"", @"orderby",
    @"", @"order",
    nil];
NSArray *postParams = [NSArray arrayWithObjects:
    [NSNumber numberWithInt:0], // blog_id
    username,
    password,
    filter,
    [NSArray arrayWithObject:@"post_title"], // fieldsArray
    nil];

Or, if you use the recent LLVM compiler that allows you to use Modern Objective-C, you can use the more concise syntax:

NSDictionary* filter = @{
    @"post_type": @"collection",
    @"post_status": @"",
    @"number": @(number)
    @"offset": @(offset)
    @"orderby": @"",
    @"order": @""};
NSArray *postParams = @[ @0, username, password, filter, @[@"post_title"]];

I even bet that for parameters that you don't want to set in your call, like the "orderby" and "order" parameters for example, you can skip the keys and avoid to set them in the dictionary.

Once again, I never used the Wordpress XML-RPC API, but as I understand the documentation, if this does not work the solution should be pretty close to something like this (for example I may be wrong and you may have to use an NSArray instead of an NSDictionary for the "filter" pseudo-struct mentioned in the doc; what you quoted from the documentation in your question is not really sufficient to tell which has to be used when using it thru Cocoa).

Upvotes: 2

Related Questions