gpt4 book ai didi

ios - 格式字符串不是字符串文字

转载 作者:行者123 更新时间:2023-11-28 20:18:44 25 4
gpt4 key购买 nike

行中有错误 totalSelection=[[NSString alloc] initWithFormat:dataView.text];错误提示 Format string is not literal,这是什么意思?

- (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{
txtTo=@"YourEmail@yahoo.com";
txtSubject=@"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 {
txtTo=@"YourEmail@yahoo.com";
txtSubject=@"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.
}

最佳答案

如果 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];

关于ios - 格式字符串不是字符串文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16822545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com