2013-05-29 150 views
0

說有totalSelection = [[NSString alloc] initWithFormat:dataView.text]的行上有錯誤;並且錯誤表示Format字符串不是字面的,這是什麼意思?格式化字符串不是字符串文字

- (void)viewDidLoad { 
    clientName= [[NSArray alloc] initWithObjects:@"Bob",@"Pete",@"Julie",@"Stacey",@"Eric", nil]; 
    anteCedent= [[NSArray alloc] initWithObjects:@"Demand",@"Alone",@"Transition",@"FreePlay",@"Eating", nil]; 
    problemBx = [[NSArray alloc] initWithObjects:@"Slap",@"Spit",@"Bite",@"Pinch",@"Threat", nil]; 
    conSequence= [[NSArray alloc] initWithObjects:@"Attention",@"Ignored",@"Escape",@"Tangible",@"Redirected", nil]; 
    [super viewDidLoad]; 
} 

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return componentCount; 
} 


//The layout of the picker view has been outlined and app can interpret the code 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component{ 
    if (component==clientComponent){ 
     return [clientName count]; 
    } 
    if (component== bxComponent){ 
     return [problemBx count]; 
    } 

    if (component== antComponent){ 
     return [anteCedent count]; 
    } 

    if (component== conComponent){ 
     return [conSequence count]; 
    } 
} 

//Enables the app to correctly identify the number corresponding to each row 
-(NSString *)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component{ 
    if (component==clientComponent){ 
     return [clientName objectAtIndex:row]; 
    } 
    if (component==bxComponent) { 
     return [problemBx objectAtIndex:row]; 
    } 
    if (component==antComponent) { 
     return [anteCedent objectAtIndex:row]; 
    } 
    if (component==conComponent){ 
     return [conSequence objectAtIndex:row]; 
    } 
} 

-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger) component{ 
    NSString *Clientmessage; 
    NSString *BXmessage; 
    NSString *anteMessage; 
    NSString *conMessage; 
    if (component==clientComponent){ 
     Clientmessage=[[NSString alloc] initWithFormat:@"%@",[clientName objectAtIndex:row]]; 
     lblClient.text=Clientmessage; 
    } 
    if (component==bxComponent){ 
     BXmessage=[[NSString alloc] initWithFormat:@"%@",[clientName objectAtIndex:row]]; 
     lblBX.text=BXmessage; 
    } 
    if (component==antComponent){ 
     anteMessage=[[NSString alloc] initWithFormat:@"%@",[clientName objectAtIndex:row]]; 
     lblAnte.text=anteMessage; 
    } 
    if (component==conComponent){ 
     conMessage=[[NSString alloc] initWithFormat:@"%@",[clientName objectAtIndex:row]]; 
     lblBX.text=conMessage; 
    } 
} 

//Set the time and date and adding information 
-(IBAction)EnterSelection:(id)sender;{ 
    NSString *totalSelection; 
    NSDate *date1 = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; 
    NSString *myDate = [dateFormatter stringFromDate:date1]; 
    totalSelection=[[NSString alloc] initWithFormat:dataView.text]; 
    totalSelection=[totalSelection stringByAppendingString:@"\n"]; 
    totalSelection=[totalSelection stringByAppendingString:lblClient.text]; 
    totalSelection=[totalSelection stringByAppendingString:@","]; 
    totalSelection=[totalSelection stringByAppendingString:lblAnte.text]; 
    totalSelection=[totalSelection stringByAppendingString:@","]; 
    totalSelection=[totalSelection stringByAppendingString:lblBX.text]; 
    totalSelection=[totalSelection stringByAppendingString:@","]; 
    totalSelection=[totalSelection stringByAppendingString:lblCon.text]; 
    totalSelection=[totalSelection stringByAppendingString:@","]; 
    totalSelection=[totalSelection stringByAppendingString:myDate]; 
    dataView.text=totalSelection; 
} 

//sets the code that will automatically enetered as the recipient of the email 
-(void) send:(id) sender{ 
    [email protected]"[email protected]"; 
    [email protected]"ABC Data"; 
    [self sendEmailTo:txtTo withSubject:txtSubject withBody:[dataView text]]; 
} 

//Organizes all email info and calls up mail function of iPhone 
-(void) sendEmailTo: (NSString*)to withSubject:(NSString*)subject withBody:(NSString*)body { 
    [email protected]"[email protected]"; 
    [email protected]"ABC Data"; 
    NSString *mailString= [NSString stringWithFormat:@"mailto:ff?to=%@&subject=%@&body=%@", 
     [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
     [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding], 
     [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]; 

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]]; 
} 

// Do any additional setup after loading the view, typically from a nib. 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
+0

將「YYYY-MM-dd」更改爲「yyyy-MM-dd」以避免在新的一年中顯示錯誤的年份。 – Hafthor

回答

4

如果dataView.text包含%?然後initWithFormat:會將其解釋爲格式規範的開始,並且(可能)嘗試查看您未在消息中傳遞的參數。您可能會崩潰或損壞數據。這種類型的錯誤非常常見且嚴重,編譯器被編程爲檢測風險並警告您。

由於您不想將dataView.text用作格式字符串,因此請勿將它傳遞給initWithFormat:。這條線會更好這樣寫的:

totalSelection = [dataView.text copy]; 

你甚至可以重寫整個字符串附加部分是這樣的:

totalSelection = [dataView.text stringByAppendingString:@"\n"]; 
    NSString *fields = [@[ 
     lblClient.text, lblAnte.text, lblBX.text, lblCon.text, myDate 
    ] componentsJoinedByString:@","]; 
    totalSelection = [totalSelection stringByAppendingString:fields]; 
+0

將代碼更改爲該代碼並運行模擬器之後,它會在應用程序的最後爲每個選擇器列重複進行數據選擇。 – Ryanasto1

+0

我不明白「它只是在應用程序結束時爲每個數據選擇重複每個選擇器列。」 –

+0

我無法發佈模擬器b/c的屏幕截圖我沒有足夠的聲望,但我正在說話關於數據部分(在底部),客戶應該說鮑勃,皮特應該說獨自,埃裏克應該說吐,結果應該說Esc ...他們應該對應每個點擊者列。它沒有提供正確的信息@robmayoff – Ryanasto1

-1

試試這個。 initWithFormat:@「%@」將忽略任何格式。

//Set the time and date and adding information 
-(IBAction)EnterSelection:(id)sender;{ 
    NSString *totalSelection = [[NSString alloc] initWithFormat:@"%@", dataView.text]; 
    NSDate *date1 = [NSDate date]; 
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; 
    NSString *myDate = [dateFormatter stringFromDate:date1]; 
    totalSelection=[totalSelection stringByAppendingString:@"\n"]; 
    totalSelection=[totalSelection stringByAppendingString:lblClient.text]; 
    totalSelection=[totalSelection stringByAppendingString:@","]; 
    totalSelection=[totalSelection stringByAppendingString:lblAnte.text]; 
    totalSelection=[totalSelection stringByAppendingString:@","]; 
    totalSelection=[totalSelection stringByAppendingString:lblBX.text]; 
    totalSelection=[totalSelection stringByAppendingString:@","]; 
    totalSelection=[totalSelection stringByAppendingString:lblCon.text]; 
    totalSelection=[totalSelection stringByAppendingString:@","]; 
    totalSelection=[totalSelection stringByAppendingString:myDate]; 
    dataView.text=totalSelection; 
}