這是在Objective C中編寫的代碼,我將值初始化爲參數化的構造函數。儘管所有字段都正確輸入,但我在輸入NSDate
字段時遇到問題。下面給出的是構造函數聲明,實現和最後的方法調用。如何在目標C中的參數化構造函數中初始化NSDate?
//初始化聲明
-(instancetype)initWithParam1:(NSNumber*)customerId_ withParam2: (NSString*)firstName_ withParam3:(NSString*)lastName_ withParam4:(NSDate*)dateOfBirth_ withParam5:(NSString*)address_ withParam6:(NSNumber*)mobileNumber_;
//初始化定義
-(instancetype)initWithParam1:(NSNumber *)customerId_ withParam2:(NSString *)firstName_ withParam3:(NSString *)lastName_ withParam4:(NSDate *)dateOfBirth_ withParam5:(NSString *)address_ withParam6:(NSNumber *)mobileNumber_
{
self = [super init];
if(self)
{
self.customerId = customerId_;
self.firstName = firstName_;
self.lastName = lastName_;
self.dateOfBirth = dateOfBirth_;
self.address = address_;
self.mobileNumber = mobileNumber_;
}
return self;
//初始化呼叫
Customer *c1 = [[Customer alloc]initWithParam1:@1001 withParam2:@"Aman" withParam3:@"Zaidi" withParam4:@"22-05-1993" withParam5:@"Bangalore" withParam6:@9567812345];
如何在字段中輸入日期的任何建議。對不起,對於天真,但我對Objective C完全陌生,而且我已經習慣了語法。
'@ 「22-05-1993」'是一個' NSString',而不是'NSDate'。 – rmaddy
您傳遞給'param6'的值是'NSNumber',而不是'NSString'。電話號碼應該是字符串,而不是數字。你不會在電話號碼上進行數學運算。 – rmaddy
感謝您的更正。 :) – jRapp3r