2010-09-19 65 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

相關問題