2013-05-29 2 views
0

totalSelection = [[NSString alloc] initWithFormat : dataView.text]; 줄에 오류가 있습니다. 오류가 Format string이 리터럴이 아니라고 말하면 어떻게됩니까?형식 문자열이 문자열 리터럴이 아닙니다

- (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

%이 포함되면 어떻게 될까요? 그런 다음 initWithFormat:은이를 형식 스펙의 시작으로 해석하고 (아마도) 메시지에서 전달하지 않은 인수를 조사합니다. 데이터가 손상되거나 손상 될 수 있습니다. 이 유형의 버그는 컴파일러가 위험을 감지하고 경고하도록 프로그래밍되는 매우 일반적이며 심각합니다.

dataView.text을 형식 문자열로 사용하고 싶지 않으므로 initWithFormat:으로 전달하지 마십시오. 그 줄은 더 나은 다음과 같이 기록 될 것입니다 :

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 충분한 평판이 없지만 이야기하고 있습니다. 데이터 섹션 (하단)에 대해 클라이언트는 Bob에게 말해야합니다, Pete는 혼자라고 말하고, Eric은 침을 말해야합니다. 결과는 Esc라고해야합니다 ... 각 clicker 열에 해당해야합니다. 올바른 정보를 제공하지 않습니다. @robmayoff – Ryanasto1

-1

이 시도 :

totalSelection = [dataView.text copy]; 

당신은이 같은 전체 문자열 추가 섹션을 다시 작성할 수 있습니다. 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; 
} 
관련 문제