2010-09-19 66 views
2

我有一个用户类,我通过iPhone应用程序使用,这是从我的用户类(SUBBLASS OF NSobject)的init和initWithUser函数,当我使用initWithUser函数时,我得到警告在代码后面描述。请指教。不兼容Objective-c类型警告

// 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类型的结构User *',e xpected“结构的NSString *”路过时的参数1“initWithUser:”从不同的Objective-C型

任何帮助和指导表示赞赏

+3

嗯。你可以发布'User.h'文件吗? – Yuji 2010-09-19 03:24:19

+0

'Serialize'是什么样的?你有什么奇怪的“NSObject”类别?任何可能涉及的'#define'? – 2010-09-19 03:37:36

+0

我说上面的序列化的代码,没有的#define或类似的东西 – curiousMo 2010-09-19 03:56:40

回答

11

的问题是你有一个模糊的选择。由于alloc返回id,致initWithUser:已变得模糊不清。 NSUserDefaults也有一个接受字符串的initWithUser:函数。编译器认为你正在尝试使用那个。将该行更改为

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

并且所有内容都应按预期工作。

+0

太感谢你了,我的名字改为initWithAUser消除不确定性。我不会想到这一点,你为我节省了很多挫折。并感谢大家的帮助。 – curiousMo 2010-09-19 13:13:31

+1

良好的通话。在我看来,编译器警告应该能够明确表示它是模糊的,而不是参数是错误的类型,因为没有特别的理由更喜欢NSUserDefault而不是User。 – imaginaryboy 2010-09-19 16:51:35

+1

我认为铿锵可能对这种情况有更好的警告。在一个完美的世界中,它将能够看到你指定的类型并选择返回该类型的正确选择器。 – 2010-09-19 18:22:11

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例如,如果newUsercopyretain财产有一个release缺失:

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

感谢您花时间创建此实现。 – curiousMo 2010-09-20 22:58:26

相关问题