Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何指定格式化的NSString常量_Iphone_Ios_Nsstring_Uikit_Stringwithformat - Fatal编程技术网

Iphone 如何指定格式化的NSString常量

Iphone 如何指定格式化的NSString常量,iphone,ios,nsstring,uikit,stringwithformat,Iphone,Ios,Nsstring,Uikit,Stringwithformat,我有一些预定义的字符串格式,我希望在运行时向其填充值,并将其呈现给用户。我想将它们存储为常量或定义,以更合适的为准 比如: @% has sent you a message on @%-@%-@%. 在运行时,上面将获取发送方的名称和mm dd yy,以准备所需的字符串: James Bond has sent you a message on 12-31-2050. 有人能指出实现这种事情的代码吗 编辑: 与许多人,尤其是下层选民的想法相反!相信: 这不仅仅是一个问题。张贴该报告是为了确

我有一些预定义的字符串格式,我希望在运行时向其填充值,并将其呈现给用户。我想将它们存储为常量或定义,以更合适的为准

比如:

@% has sent you a message on @%-@%-@%.
在运行时,上面将获取发送方的名称和mm dd yy,以准备所需的字符串:

James Bond has sent you a message on 12-31-2050.
有人能指出实现这种事情的代码吗

编辑: 与许多人,尤其是下层选民的想法相反!相信:

这不仅仅是一个问题。张贴该报告是为了确保:

字符串格式存储是内存高效的静态存储,而不是定义存储,因为我希望永久存储格式,而不仅仅是为了使用时间 与其他选项相比,运行时的值替换是时间高效的stringWithFormat
使用[NSString stringWithFormat:@%@已在%@,who,date向您发送消息]

试试这个。。也许对你有帮助

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MM-yyyy"];
    NSDate *now = [[NSDate alloc] init];

    NSString *theDate = [dateFormat stringFromDate:now];
     NSLog(@"theDate..%@",theDate);
    NSString *str = @"yourString";
    NSString *new =  [NSString stringWithFormat:@"%@ has sent you a message on %@", str, theDate];
您可以这样做:

在顶部

#define MESSAGE @"%@ has sent you a message on %@-%@-%@."
然后,每当您想使用该消息时,请使用以下syntex:

[NSString stringWithFormat:MESSAGE, <sender_name>, <date>, <month>, <year>];
发件人名称、日期、月份、年份是动态值

霍普,你有主意了

快乐编码

干杯

使用
定义messageStringx,y,z[NSStringStringWithFormat:@%@已在%@-%@-%@,x,y,z上向您发送消息]

首先,您需要获取当前日期

NSDateFormatter *date= [[NSDateFormatter alloc] init];
    [date setDateFormat:@"MM-dd-yyyy"];
    NSDate *thisDate= [[NSDate alloc] init];

NSString *currentDate= [date stringFromDate:thisDate];
 NSLog(@"Current date is..%@",currentDate);
现在,您可以在运行时输入任何字符串

 NSString *strng = @"whatever kind of string which you want to add at runtime";
    NSString *newString=  [NSString stringWithFormat:@"%@ has sent you a message on %@", strng, currentDate];

newString的值将为您提供所需的结果。

请检查@%-@%-@%的日期格式设置方法,这对您来说是非常糟糕的localization@MidhunMP,问题更多的是有一个常数,而不仅仅是格式。大卫,这个格式只是样本。无论谁投了反对票,请说明理由。我看到很多潜在用户都在寻求这个解决方案,所以除非这个问题有重复的地方,否则应该进行升级投票。你需要NSDateFormatter来表示日期,只是%@看起来不太好