Objective-cを使用してプログラミングしています。私はHTTP POST Methodを使ってリクエストしたい。私が送るパラメータはint型である。__NSCFNumber stringByAddingPercentEncodingWithAllowedCharacters認識できないセレクタエラー
私はこのエラーを取得しています:このコード行で
[__NSCFNumber stringByAddingPercentEncodingWithAllowedCharacters:]: unrecognized selector sent to instance 0xb0000000000048d3
:
return [string stringByAddingPercentEncodingWithAllowedCharacters:allowed];
全体コード:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
request.HTTPMethod = @"POST";
[request setHTTPBody:[self httpBodyForParameters:params]];
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"dataTaskWithRequest error: %@", error);
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
NSLog(@"Expected responseCode == 200; received %ld", (long)statusCode);
}}
NSError *parseError;
id responseObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
if (!responseObject) {
NSLog(@"JSON parse error: %@", parseError);
} else {
NSLog(@"responseObject = %@", responseObject);
}
NSLog(@"print123");
}];
[task resume];
}
- (NSData *)httpBodyForParameters:(NSDictionary *)parameters {
NSMutableArray *parameterArray = [NSMutableArray array];
[parameters enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
NSString *param = [NSString stringWithFormat:@"%@=%@", [self percentEscapeString:key], [self percentEscapeString:obj]];
[parameterArray addObject:param];
}];
NSString *string = [parameterArray componentsJoinedByString:@"&"];
return [string dataUsingEncoding:NSUTF8StringEncoding];
}
- (NSString *)percentEscapeString:(NSString *)string {
NSCharacterSet *allowed = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-._~"];
return [string stringByAddingPercentEncodingWithAllowedCharacters:allowed];
}
一つは 'NSNumber'、ない' NSStringのです'。 – Paulw11