Open In App

Text and strings Storage in Objective C

Last Updated : 01 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Objective-C is an object-oriented programming language widely used to develop programs for iOS and macOS. This article focuses on discussing how text and string data can be stored in Objective-C. 

Types and Subtypes of Strings

Objective-C has two main types of strings – NSString and NSMutableString. An NSString object is immutable and contains a sequence of Unicode characters that cannot be modified after creation. On the other hand, an NSMutableString object is mutable, which means that its contents can be modified.

There are also several subtypes of NSString, each with its specific use case. These subtypes include:

  1. C Strings.
  2. NSString Literals.
  3. String Variables.

1. C Strings

A C string is a sequence of characters stored in a character array. In Objective-C, a C string is represented by a pointer to the first element of the character array. C strings are null-terminated, meaning that a null character (‘\0’) marks the end of the string.

In this example we are going to understand the C string in Objective-C:

ObjectiveC




// Objective-C program to implement
// C Strings
#import <Foundation/Foundation.h>
 
int main()
  // Declare a character array named "myString"
  // and initialize it with the string "Hello, world!"
  char myString[] = "Hello, world!";
 
  // Print the contents of the "myString" array to
  // the console using the "NSLog" function
  NSLog(@"%s", myString);
  return 0;
}


Output:

C strings

 

2. NSString Literals

An NSString literal is a sequence of characters enclosed in double quotes. In Objective-C, NSString literals are represented by the @ symbol followed by the string enclosed in double quotes.

In this example, we will understand the NSString literal in Objective-C.

ObjectiveC




// Objective-C program to implement
// NSString Literals
#import <Foundation/Foundation.h>
 
int main()
{  
  // Create an NSString object named "myString1"
  // and initialize it with the string "Hello, world!"
  NSString *myString1 = @"Hello, world!";
   
  // Print the value of "myString1" using NSLog
  NSLog(@"myString1: %@", myString1);
   
  // Return 0 to indicate successful completion of the program
  return 0;
}


Output:
 

NSString Literals

 

3. String Variables

A string variable is a variable that stores a string value. In Objective-C, string variables are declared using the NSString or NSMutableString class.

In this example, we are going to understand string variables in Objective-C.

ObjectiveC




// Objective-C program to implement
// string variables
int main()
{   
  // Create an NSString object named "myString2"
  // and initialize it with the string "Hello, world!"
  NSString *myString2 = @"Hello, world!";
   
  // Create an NSMutableString object named "myMutableString"
  // and initialize it with the string "Hello, world!"
  NSMutableString *myMutableString = [NSMutableString stringWithString:@"Hello, world!"];
   
  // Print the value of "myString2" using NSLog
  NSLog(@"myString2: %@", myString2);
   
  // Print the value of "myMutableString" using NSLog
  NSLog(@"myMutableString: %@", myMutableString);
   
  // Return 0 to indicate successful completion of the program
  return 0;
}


Output:

String variables



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads