Format-Specifiers

suggest change

Introduction

Format-Specifiers are used in Objective-c to implant object-values into a string.

Syntax

Remarks

Due to the nature of format-specifiers, if you wish to include the percentage symbol (%) in your string, you must escape it using a second percentage symbol.

Example:

int progress = 45;//percent
NSString *progressString = [NSString stringWithFormat:@"Progress: %i%%", (int)progress];

NSLog(progressString);//logs "Progress: 45%"

No Format Specifier for BOOL-type exists.

Common-use solutions include:

BOOL myBool = YES;
NSString *boolState = [NSString stringWithFormat:@"BOOL state: %@", myBool?@"true":@"false"];

NSLog(boolState);//logs "true"

Which utilizes a ternary operator for casting a string-equivalent.

BOOL myBool = YES;
NSString *boolState = [NSString stringWithFormat:@"BOOL state: %i", myBool];

NSLog(boolState);//logs "1" (binary)

Which utilizes an (int) cast for implanting a binary-equivalent.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents