在创建尝试将对象从数组保存到rails后端时,我遇到了一个奇怪的情况。我有两个数组,我自己保存每个数据没有问题。问题是我也在创建一个表,它是两个对象的组合。因此,最好为两个数组创建索引路径1的操作,为两个数组创建索引路径2,依此类推。数组中的对象总是具有相同数量的对象。这样,对于索引路径一,它可以同时保存它们并组合表,从而消除了我的问题。我的第二个问题是我不知道如何为多个数组的索引路径创建一个代码块。我知道这可能很奇怪,但任何帮助都会很棒。
以下是我为单个数组的每个对象执行此操作的方法:
-(void)savePeroid {
[self.periodArray enumerateObjectsUsingBlock:^(id object, NSUInteger idx, BOOL *stop) {
if (self.navigationItem.rightBarButtonItem.tintColor == [UIColor redColor]) {
NSURL *url = [NSURL URLWithString:@"http://localhost:3000/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
object, @"period[name]",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"api/period"
parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"ID: %@", [JSON valueForKeyPath:@"id"]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failure Because %@",[error userInfo]);
}];
[operation start];
}
}];
[self saveSubject];
}
答案 0 :(得分:0)
这个怎么样:
-(void)savePeroid {
for (int i=0; i<self.periodArray.count; i++) {
id periodObject = self.periodArray[i];
id otherArrayObject = self.otherArray[i];
if (self.navigationItem.rightBarButtonItem.tintColor == [UIColor redColor]) {
NSURL *url = [NSURL URLWithString:@"http://localhost:3000/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
periodObject, @"period[name]",
otherArrayObject, @"other[something]",
nil];
NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST"
path:@"api/period"
parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"ID: %@", [JSON valueForKeyPath:@"id"]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failure Because %@",[error userInfo]);
}];
[operation start];
}
};
[self saveSubject];
}