Cocoa POST问题

时间:2016-04-18 21:36:57

标签: cocoa post nsurlconnection nsmutableurlrequest

我是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];
}

我该如何解决?

谢谢&amp;此致

1 个答案:

答案 0 :(得分:0)

在包含request方法的.m文件中,您需要指明该类符合DelegadoRedPost协议并实现所需的协议方法。

@implementation行之前添加此内容:

@interface WhateverClassNameThisIs () <DelegadoRedPost>

@end

显然将WhateverClassNameThisIs替换为此类的实际名称。

作为旁注,您应该更改delegado属性的声明:

@property (strong, nonatomic) NSObject <DelegadoRedPost> *delegado;

为:

@property (weak, nonatomic) id<DelegadoRedPost> *delegado;

请注意两项更改 - 代理通常应为weak,而不是strong。这避免了参考周期。类型应该是id,而不是NSObject。协议本身扩展了NSObject协议。