2012-09-15 47 views
0

我想用wordpress的xmlrpc来获取特定自定义类型(称为集合)的文章。iOS中的结构与XML-RPC请求

WordPress的API文档状态:

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. 

我的问题是在形成与objc串的结构:

我想要做这样的事情:

// 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]); 
} 

回答

2

我不知道XML-RPC WordPress API,但是structs是C构造,但不是Cocoa NSObjects,所以你不能将它们嵌入NSArray

所以[NSArray arrayWithObjects:..., filter, ..., nil]行无效,因为filterwp_filter结构。

我认为在API文档中提到的“结构”是更多的伪代码来解释数据结构/组织。显然你需要将该概念“翻译”成Cocoa对象。

你可能会尝试的是将你的C结构转换为NSArray并且按照API期望的顺序传递参数,或者更有可能将你的结构转换成NSDictionary,其键是结构体的名称字段和值显然是您的结构的字段值。

此外,要将整数封装到Cocoa对象中,您应该使用NSNumber类,而不是将整数转换为NSStrings


所以这个前人的精力给这样的事情:

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]; 

或者,如果您使用最近LLVM编译器,它可以让你用现代的Objective-C,你可以用更简洁的语法:

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

我甚至打赌,对于你不想在你的电话设置参数,如"orderby""order"参数,例如,你可以跳过键并避免将它们设置在字典中。

再次,我从来没有使用Wordpress XML-RPC API,但据我了解的文档,如果这不起作用的解决方案应该是非常接近这样的事情(例如,我可能是错的,你可能有要使用NSArray而不是NSDictionary作为文档中提到的“过滤器”伪结构;你在问题中引用的文档不足以说明在通过Cocoa使用时必须使用哪一个)。

+0

非常感谢您的帮助。但是,我已经尝试了NSArray&使用NSDictionary,因为您建议似乎被响应忽略(即返回所有非特定帖子类型的帖子)。我将进一步研究XMLRPCRequest类,以了解它如何格式化参数。 – thepaperboy

+0

虽然我似乎无法得到它正确响应,它似乎以为你的答案是正确的,xmlrpc编码器将键值对格式化为名为的包装 – thepaperboy