Cocoa POST 问题
Cocoa POST issue
我是 Cocoa 的新手。当我想通过 POST
向我的 WS 发送数据时遇到一个问题
我的所有项目都继承了 RequestPost 程序
//
// RequestPost.h
//
// Created by Roberto on 10/01/13.
// Copyright (c) 2013 CEM. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol DelegadoRedPost <NSObject>
-(void) terminaDescarga:(NSData*)datos conID:(NSInteger) id;
-(void) errorDescarga:(NSInteger)codigo conID:(NSInteger) id;
@end
@interface RequestPost : NSObject <NSURLConnectionDelegate>
@property (strong, nonatomic) NSObject <DelegadoRedPost> *delegado;
@property (nonatomic) NSInteger id;
@property (nonatomic, strong) NSMutableData *buffer;
@property (nonatomic, strong) NSURLConnection *conexion;
-(void)descargar:(NSString*)direccion datosPost:(NSString*)datos conId:(NSInteger)id;
@end
//
// RequestPost.m
//
// Created by Roberto on 10/01/13.
// Copyright (c) 2013 CEM. All rights reserved.
//
#import "RequestPost.h"
@implementation RequestPost
-(void)descargar:(NSString*)direccion datosPost:(NSString*)datos conId:(NSInteger)id
{
self.id = id;
NSURL *url = [NSURL URLWithString:direccion];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
//NSString *strLength = [NSString stringWithFormat:@"%d", datos.length]; aqui comento 18 abr 2016
NSString *strLength = [NSString stringWithFormat:@"%lu", (unsigned long)datos.length];
[req addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req addValue:strLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[datos dataUsingEncoding:NSUTF8StringEncoding]];
self.conexion = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if(self.conexion){
self.buffer = [NSMutableData data];
}
}
#pragma mark - Métodos del Delegado de NSURLConnection
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.buffer.length = 0;
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.buffer appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
[self.delegado terminaDescarga:self.buffer conID:self.id];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[self.delegado errorDescarga:error.code conID:self.id];
}
@end
现在,当我想继承最后一个文件时,我遇到了错误....分配给 NSObject 的指针类型不兼容...在行 request.delegado = self;
这是继承时的代码
-(void) request
{
RequestPost *request = [[RequestPost alloc] init];
request.delegado = self;
NSString *postStr = [NSString stringWithFormat:@"datos=%@",self.json];
NSString *strUrl = @"http://www.futho7.com/WebService/subir_datos.php";
[request descargar:strUrl datosPost:postStr conId:100];
}
我该如何解决?
感谢和问候
在包含request
方法的.m文件中,需要注明class符合DelegadoRedPost
协议,并实现需要的协议方法
在 @implementation
行之前添加:
@interface WhateverClassNameThisIs () <DelegadoRedPost>
@end
显然用这个 class 的实际名称替换 WhateverClassNameThisIs
。
作为旁注,您应该将 delegado
属性 的声明从:
更改为
@property (strong, nonatomic) NSObject <DelegadoRedPost> *delegado;
至:
@property (weak, nonatomic) id<DelegadoRedPost> *delegado;
请注意两个变化 - 代表通常应该是 weak
,而不是 strong
。这避免了引用循环。类型应该是id
,而不是NSObject
。该协议本身扩展了 NSObject
协议。
我是 Cocoa 的新手。当我想通过 POST
向我的 WS 发送数据时遇到一个问题我的所有项目都继承了 RequestPost 程序
//
// RequestPost.h
//
// Created by Roberto on 10/01/13.
// Copyright (c) 2013 CEM. All rights reserved.
//
#import <Foundation/Foundation.h>
@protocol DelegadoRedPost <NSObject>
-(void) terminaDescarga:(NSData*)datos conID:(NSInteger) id;
-(void) errorDescarga:(NSInteger)codigo conID:(NSInteger) id;
@end
@interface RequestPost : NSObject <NSURLConnectionDelegate>
@property (strong, nonatomic) NSObject <DelegadoRedPost> *delegado;
@property (nonatomic) NSInteger id;
@property (nonatomic, strong) NSMutableData *buffer;
@property (nonatomic, strong) NSURLConnection *conexion;
-(void)descargar:(NSString*)direccion datosPost:(NSString*)datos conId:(NSInteger)id;
@end
//
// RequestPost.m
//
// Created by Roberto on 10/01/13.
// Copyright (c) 2013 CEM. All rights reserved.
//
#import "RequestPost.h"
@implementation RequestPost
-(void)descargar:(NSString*)direccion datosPost:(NSString*)datos conId:(NSInteger)id
{
self.id = id;
NSURL *url = [NSURL URLWithString:direccion];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
//NSString *strLength = [NSString stringWithFormat:@"%d", datos.length]; aqui comento 18 abr 2016
NSString *strLength = [NSString stringWithFormat:@"%lu", (unsigned long)datos.length];
[req addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[req addValue:strLength forHTTPHeaderField:@"Content-Length"];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[datos dataUsingEncoding:NSUTF8StringEncoding]];
self.conexion = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if(self.conexion){
self.buffer = [NSMutableData data];
}
}
#pragma mark - Métodos del Delegado de NSURLConnection
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
self.buffer.length = 0;
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.buffer appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
[self.delegado terminaDescarga:self.buffer conID:self.id];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
[self.delegado errorDescarga:error.code conID:self.id];
}
@end
现在,当我想继承最后一个文件时,我遇到了错误....分配给 NSObject 的指针类型不兼容...在行 request.delegado = self;
这是继承时的代码
-(void) request
{
RequestPost *request = [[RequestPost alloc] init];
request.delegado = self;
NSString *postStr = [NSString stringWithFormat:@"datos=%@",self.json];
NSString *strUrl = @"http://www.futho7.com/WebService/subir_datos.php";
[request descargar:strUrl datosPost:postStr conId:100];
}
我该如何解决?
感谢和问候
在包含request
方法的.m文件中,需要注明class符合DelegadoRedPost
协议,并实现需要的协议方法
在 @implementation
行之前添加:
@interface WhateverClassNameThisIs () <DelegadoRedPost>
@end
显然用这个 class 的实际名称替换 WhateverClassNameThisIs
。
作为旁注,您应该将 delegado
属性 的声明从:
@property (strong, nonatomic) NSObject <DelegadoRedPost> *delegado;
至:
@property (weak, nonatomic) id<DelegadoRedPost> *delegado;
请注意两个变化 - 代表通常应该是 weak
,而不是 strong
。这避免了引用循环。类型应该是id
,而不是NSObject
。该协议本身扩展了 NSObject
协议。