不兼容的Objective-c类型警告

时间:2010-09-19 03:09:49

标签: objective-c ios4

我有一个用户类,我通过iPhone应用程序使用,这是我的用户类(一个SUBCLASS OF NSobject)的init和initWithUser函数,当我使用initWithUser函数时,我得到代码后面描述的警告。请指教。

// serialize.h 
#import <Foundation/Foundation.h>

@protocol Serialize

// serialize the object to an xml string
-(NSString*)ToXML;

@end


// user.h
#import <Foundation/Foundation.h>
#import "Serialize.h"
#import "Contact.h"


@interface User : NSObject <Serialize> {

NSString *email;
NSString *firstName;
NSString *lastName;
NSString *userId;
NSString *userName;
NSString *password;

NSMutableArray *contactList;

}

@property (nonatomic,copy) NSString *email;
@property (nonatomic,copy) NSString *firstName;
@property (nonatomic,copy) NSString *lastName;
@property (nonatomic,copy) NSString *userId;
@property (nonatomic,copy) NSString *userName;
@property (nonatomic,copy) NSString *password;
@property (nonatomic, retain) NSMutableArray *contactList;

//-(id)init;
-(id)initWithUser:(User *)copyUser;

@end



// user.m 
#import "user.h"


@implementation User

@synthesize email;
@synthesize firstName;
@synthesize lastName;
@synthesize userId;
@synthesize userName;
@synthesize password;
@synthesize contactList;

-(id)init
{
    // call init in parent and assign to self
    if( (self = [super init]) ) 
    {          
        // do something specific 
        contactList = [[NSMutableArray alloc] init];

    }
    return self;
}

-(id)initWithUser:(User *)copyUser
{
    if( (self = [self init]) ) {           

        email               = copyUser.email;
        firstName           = copyUser.firstName;
        lastName            = copyUser.lastName;
        userId              = copyUser.userId;
        userName            = copyUser.userName;
        password            = copyUser.password;

        // release contactList initialized in the init
        [contactList release];
        contactList         = [copyUser.contactList mutableCopy];
    }

    return self;
}

- (void)dealloc
{
    // TODO: 
    [contactList removeAllObjects];
    [contactList release];
    [super dealloc];
}

// implementation of serialize protocol
-(NSString*)ToXML
{
    return @""; 
}

我在主控制器中以这种方式使用它

- (void) registerNewUser {

    RegistrationViewController *regController = [[RegistrationViewController alloc] init] ;

    regController.newUser = [[User alloc] initWithUser:self.user];

    [self.navigationController pushViewController:regController animated:YES];
    [regController release];

}

regController.newUser = [[User alloc] initWithUser:self.user];

给了我以下错误,它让我疯了几天:

  

不兼容的Objective-c类型'struct User *',当从不同的Objective-c类型传递'initWithUser:'的参数1时,预期'struct NSString *'

感谢任何帮助和指导

2 个答案:

答案 0 :(得分:11)

问题是你有一个模棱两可的选择器。由于alloc会返回id,因此对initWithUser:的调用变得模棱两可。 NSUserDefaults还有一个initWithUser:函数,它接受一个字符串。编译器认为你正在尝试使用那个。将行更改为

regController.newUser = [(User*)[User alloc] initWithUser:self.user];

并且一切都应该按预期工作。

答案 1 :(得分:1)

如评论中所述,您的实施还存在其他问题。在初始化程序中,重复使用-init是多余的,而email之类的ivars的分配应使用-copy-retain来获取数据的所有权:

-(id)initWithUser:(User *)copyUser {
    if((self = [super init])) {
        // take ownership of the users data by copying or retaining:
        email = [copyUser.email copy];
        // ...

        contactList = [copyUser.contactList mutableCopy];
    }    
    return self;
}

-dealloc中,-removeAllObjects可以删除,并且必须发布成员数据:

- (void)dealloc {
    [email release];
    // ...

    [contactList release];
    [super dealloc];
}

请注意,如果User属于newUsercopy,则您还会泄漏新retain个实例,因为缺少release

User *user = [[User alloc] initWithUser:self.user];
regController.newUser = user;
[user release];