ios - 什么时候使用 NSString stringWithFormat;

标签 ios objective-c nsstring

当我阅读如何在 iOS 中使用 Sqlite 时,我遇到了以下行

NSString *query = [NSString stringWithFormat:@"SELECT * FROM 
tableName"];

我搜索了一些教程和文档以找出为什么使用以下方法而不是常规 NSString 对象:

stringWithFormat

为什么我们不能使用像下面这样的东西,因为不需要指定和特殊格式,如 %@ 或 %d?:

NSString *query = @"SELECT * FROM tableName";

最佳答案

如果您不打算从变量中获取值,@"SELECT * FROM tableName" 这应该没问题。

如果你打算在运行时替换表名,你会使用stringWithFormat

NSString *query = [NSString stringWithFormat:@"SELECT * FROM %@", myTableName];

关于ios - 什么时候使用 NSString stringWithFormat;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44539979/

相关文章:

objective-c - 将 NSString 与常量进行比较 #define string : isEqualToString or ==?

iphone - iPad Retina 登陆页面大小 - 2MB

ios - 避免在 SwiftUI 中为其内容设置按钮样式

ios - 一个属性字符串如数组

objective-c - UITableView 滑动删除在 iOS8 上无法正常工作

objective-c - 能够在后台线程中使用UIKit类

objective-c - 将字符串转换为 "Method"- 用于类方法调用

ios - 在 WatchKit WKInterfaceTable 的表格行中布置对象

ios - UITapGestureRecognizer 部分适用于 UITableViewCell 内的 UIImageView

iphone - 从 CLLocationDegrees 生成一个字符串,例如在 NSLog 或 StringWithFormat 中