Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
Ios &引用;格式字符串不是字符串文字(可能不安全)";_Ios_Objective C_Iphone - Fatal编程技术网

Ios &引用;格式字符串不是字符串文字(可能不安全)";

Ios &引用;格式字符串不是字符串文字(可能不安全)";,ios,objective-c,iphone,Ios,Objective C,Iphone,我正在创建一个显示消息、用户和时间戳的学校应用程序。我试着在网上寻找解决办法,但我找不到任何我能理解的东西。我是编程新手,什么都可以帮我。我在某个地方读到,NSLog可能会有所帮助,但我不知道它是如何使用的。请帮忙 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier =

我正在创建一个显示消息、用户和时间戳的学校应用程序。我试着在网上寻找解决办法,但我找不到任何我能理解的东西。我是编程新手,什么都可以帮我。我在某个地方读到,NSLog可能会有所帮助,但我不知道它是如何使用的。请帮忙

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"messagesCell";
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    reversedMessages = [[messagesArray reverseObjectEnumerator] allObjects];

    PFObject *tempObject = [reversedMessages objectAtIndex:indexPath.row];

    cell.cellTitle.text = [tempObject objectForKey:@"content"];
    cell.userName.text = [tempObject objectForKey:@"user"];

    NSDate *createdAt = [tempObject createdAt];
    NSDateFormatter *dateDisplay = [[NSDateFormatter alloc] init];
    [dateDisplay setDateFormat:@"MMM, d h:mm a"];

    **cell.timeStamp.text = [NSString stringWithFormat: [dateDisplay stringFromDate:createdAt]];**

    return cell;
}

如果您使用的是stringWithFormat,则需要将带有%@的格式字符串替换为逗号分隔的参数,如下所示:

[NSString stringWithFormat:@“这是param1:%@,param2:%@”,param1,param2]

如果您使用的是普通NSString,只需执行以下操作:

cell.timeStamp.text = [dateDisplay stringFromDate:createdAt];
我想加倍强调的另一种选择是,与上述更好的方法进行对比,在这种情况下,它是无意义的:

cell.timeStamp.text = [NSString stringWithFormat:@"%@", [dateDisplay stringFromDate:createdAt]];

这毫无意义:
[NSString stringWithFormat:[dateDisplay stringFromDate:createdAt]]
——在没有替换文本的情况下使用
stringWithFormat
没有意义。如果你真的是“编程新手”,你不应该从Objective-C开始。你需要先学习C或Java,或者有很多概念你永远不会“得到”。什么是“(潜在的不安全)”?@Zaph-如果你将字符串变量作为stringWithFormat或NSLog的格式parm传递,你会得到一个编译器警告。理由是,你可以有一个SQL注入曝光的道德等价物。我没有意识到标题是一个错误消息,我现在明白了。你需要加倍强调,第二种风格完全是浪费。这是一个明显的迹象,表明某人不明白自己在做什么