Amrmsmb
Amrmsmb

Reputation: 1

When to use NSString stringWithFormat;

While I am reading how to utilize Sqlite in iOS, I reached the following line

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

I googled some tutorials and documentations to find out why the following method were used instead of a regular NSString object:

stringWithFormat

Why we could not use something like the following since there is no need to specify and special format such as %@ or %d?:

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

Upvotes: 0

Views: 467

Answers (1)

Shamas S
Shamas S

Reputation: 7549

If you aren't going to get values from variables, @"SELECT * FROM tableName" this should be fine.

If you are going to replace the name of table at runtime, you would use stringWithFormat

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

Upvotes: 3

Related Questions