In the Objective-C programming language, Objective-C data types refer to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
What are the main objective of data types?
Answer: A data type constrains the values that an expression, such as a variable or a function, might take. This data type defines the operations that can be done on the data, the meaning of the data, and the way values of that type can be stored.
What is a double in Objective-C?
The Objective-C double data type is used to store larger values than can be handled by the float data type. The term double comes from the fact that a double can handle values twice the size of a float.
What is primitive data type in Objective C?
There are two primitive floating point types available in Objective-C: The float type is a 32-bit single precision float point value that uses a single sign bit, an 8-bit mantissa and 23 bits of exponent when storing numbers.
The types in Objective-C can be classified as follows
Basic Types
-includes arithmetic types
-integer types and floating-point types
Enumerated types
-void typeindicates that no value is available
Derived types
-Pointer types
-Array types
-Structure types
-Union types
-Function types
int Data Type
int Data Type
int myoctal = 024;
char Data Type
char myChar = 'w';
Special Characters/Escape Sequences
float Data Type
float myfloat = 123.432f
double Data Type
id Data Type
BOOL Data Type
_Bool flag = 0;
BOOL secondflag = 1;
Objective-C Data Type Qualifiers
long
long int mylargeint;
long long
long long int mylargeint;
short
short int myshort;
signed / unsigned
unsigned int myint;
long double
long double mydouble;
Arrays
type arrayName [ arraySize ];
double myArray[5] = {1.0, 2.0, 3.4, 12.0, 52.0};
int n[ 10 ];
int i,j;
for ( i = 0; i < 10; i++ )
{
n[ i ] = i + 100; /* set element at location i to i + 100 */
}
for (j = 0; j < 10; j++ )
{
NSLog(@"Element[%d] = %d\n", j, n[j] );
}
NSArray *myArray;
myArray= [NSArray arrayWithObjects: object1, object2, object3, nil];
NSLog(@"Array contents = %@",[myArray componentsJoinedByString:@", "]);
NSArray *myArray;
myArray= [NSArray arrayWithObjects: object1, object2, object3, nil];
for (NSString *randomVariable in myArray)
{
NSLog (@"Array element = %@", randomVariable);
}
NSArray *myArray;
myArray= [NSArray arrayWithObjects: object1, object2, object3, nil];
int i;
for (i = 0; i < [myArray count]; i++)
{
NSLog (@"Element %i = %@", i, [myArray objectAtIndex: i]);
}
Pointers
type *var-name;
int *ip; /* pointer to an integer */
double *dp; /* pointer to a double */
float *fp; /* pointer to a float */
char *ch /* pointer to a character */
int var = 20; /* variable declaration */
int *ip; /* pointer declaration */
ip = &var; /* store address of var in pointer variable*/
NSLog(@"Address of var variable: %x\n", &var );
NSLog(@"Address stored in ip variable: %x\n", ip );
NSLog(@"Value of *ip variable: %d\n", *ip );
Strings
- (NSString *)capitalizedString;
capitalize the string.
- (unichar)characterAtIndex:(NSUInteger)index;
get the character at a given index.
- (double)doubleValue;
Converts string value to the double value.
- (float)floatValue;
Converts to float value.
- (BOOL)hasPrefix:(NSString *)aString;
Checks if the string is started with another string.
- (BOOL)hasSuffix:(NSString *)aString;
Checks if the string is ended with another string.
- (id)initWithFormat:(NSString *)format ...;
Formats a string.
- (NSInteger)integerValue;
Converts string to NSInteger value.
- (BOOL)isEqualToString:(NSString *)aString;
Compares two strings.
- (NSUInteger)length;
Returns the number of characters in the String.
- (NSString *)lowercaseString;
Lowercase the String.
- (NSRange)rangeOfString:(NSString *)aString;
Searches a sub string.
- (NSString *)stringByAppendingFormat:(NSString *)format ...;
Appends to string with format.
- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;
Trims characters from the string.
- (NSString *)substringFromIndex:(NSUInteger)anIndex ;
Gets the sub string from index.
Append sub string
NSMutableString *largeString;
largeString = [NSMutableString stringWithString: @"That is a string"];
NSLog (@"Original string = %@", largeString);
[largeString appendString: @" and this is a string too."];
NSLog (@"New string = %@", largeString);
prefix and suffixes
NSString *myName = @"John Doe";
NSString *hisName = @"John Doe";
if ([myName isEqualToString: hisName])
{
NSLog (@"The two strings are equal using isEqualToString.");
}
BOOL flag;
flag = [myName hasPrefix: @"John"];
if (flag)
{
NSLog (@"The hasPrefix method returned YES.");
}
Convert to uppercase and lowercase
NSString *testString = @"Greetings from another planet!";
NSString *targetString;
targetString = [testString uppercaseString];
NSLog (@"All uppercase = %@", targetString);
NSLog (@"**********");
targetString = [testString lowercaseString];
NSLog (@"All lowercase = %@", targetString);
NSLog (@"**********");
targetString = [testString capitalizedString];
NSLog (@"All capitalized strings = %@", targetString);
NSLog (@"**********");
NSLog (@"Original string = %@", testString);
Delete part of a string
NSMutableString *largeString;
largeString = [NSMutableString stringWithString: @"That is a string"];
NSLog (@"Original string = %@", largeString);
[largeString deleteCharactersInRange: [largeString rangeOfString: @"is a "]];
NSLog (@"New string = %@", largeString);
Extracting a Substring to the End of a String
NSMutableString *largeString;
largeString = [NSMutableString stringWithString: @"That is a string"];
NSLog (@"Original string = %@", largeString);
NSString *newString;
newString = [largeString substringFromIndex: 5];
NSLog (@"New string = %@", newString);
Extracting a Substring with Location and Length
NSMutableString *largeString;
largeString = [NSMutableString stringWithString: @"That is a string"];
NSLog (@"Original string = %@", largeString);
NSString *newString;
newString = [largeString substringWithRange: NSMakeRange(5, 4)];
NSLog (@"New string = %@", newString);
Get the length of a string
NSString *myString = @"Hello, world!";
int counter;
counter = [myString length];
NSLog (@"String length = %i", counter);
Insert a string
NSMutableString *largeString;
largeString = [NSMutableString stringWithString: @"That is a string"];
NSLog (@"Original string = %@", largeString);
[largeString insertString: @"was and still " atIndex: 5];
NSLog (@"New string = %@", largeString);
Searching and Replacing Strings
NSMutableString *largeString;
largeString = [NSMutableString stringWithString: @"That is a string"];
NSLog (@"Original string = %@", largeString);
[largeString replaceCharactersInRange: NSMakeRange(5,2) withString: @"was not"];
NSLog (@"New string = %@", largeString);
Searching and Replacing Part of a String
NSMutableString *largeString;
largeString = [NSMutableString stringWithString: @"That is a string"];
NSLog (@"Original string = %@", largeString);
[largeString replaceCharactersInRange: [largeString rangeOfString: @"is"] withString: @"was not"];
NSLog (@"New string = %@", largeString);
Searching for a Substring
NSRange myRange;
NSString *bigString = @"Learning to program can be fun!";
myRange = [bigString rangeOfString: @"can be"];
if (myRange.location == NSNotFound)
{
NSLog (@"Substring is not in %@", bigString);
}
else
{
NSLog (@"Substring found at location = %i", myRange.location);
NSLog (@"Substring length = %i", myRange.length);
}
Compare two Strings
NSString *myString1 = @"A";
NSString *myString2 = @"B";
NSString *myString3 = @"A";
BOOL isEqual = [myString1 isEqualToString:myString2];
Get a Range of String
[myString setString:@"ABCDEFGHIJKLMONPQRSTUVWXYZ"];
NSRange rangeToReplace = NSMakeRange(0, 4);
[myString replaceCharactersInRange:rangeToReplace withString:@"MORE"];
Structures
struct [structureName]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
struct Books
{
NSString *title;
NSString *author;
NSString *subject;
int id;
};
struct Books Book1; /* Declare Book1 of type Book */
Book1.title = @"Objective-C Programming";
Book1.author = @"java2s.com";
Book1.subject = @"Objective-C Programming Tutorial";
Book1.id = 1;
Typedef
typedef unsigned char BYTE;
Lists the methods from NSNumber.
+ (NSNumber *)numberWithBool:(BOOL)value
Creates and returns an NSNumber object for a BOOL value.
+ (NSNumber *)numberWithChar:(char)value
Creates and returns an NSNumber object from a signed char value.
+ (NSNumber *)numberWithDouble:(double)value
Creates and returns an NSNumber object from a double value.
+ (NSNumber *)numberWithFloat:(float)value
Creates and returns an NSNumber object from a float value.
+ (NSNumber *)numberWithInt:(int)value
Creates and returns an NSNumber object from a signed int.
+ (NSNumber *)numberWithInteger:(NSInteger)value
Creates and returns an NSNumber object from an NSInteger.
- (BOOL)boolValue
Returns the value as a BOOL.
- (char)charValue
Returns the value as a char.
- (double)doubleValue
Returns the value as a double.
- (float)floatValue
Returns the value as a float.
- (NSInteger)integerValue
Returns the value as an NSInteger.
- (int)intValue
Returns the value as an int.
- (NSString *)stringValue
Returns the value as a human-readable string.
NSNumber *myNumber;
myNumber = [NSNumber numberWithFloat:3.47];
NSLog (@"The value in NSNumber = %@", myNumber);
Convert Float NSNumber to String
float fNumber = 12;
NSString *floatToString = [NSString stringWithFormat:@"%f", fNumber];
NSLog(@"floatToString = %@", floatToString);
NSNumber *number = [NSNumber numberWithFloat:30];
NSString *numberToString = [number stringValue];
NSLog(@"numberToString = %@", numberToString);
Convert String to Number
NSString *aFloatValue = @"12.50";
float f = [aFloatValue floatValue];
float result = f * 2 + 45;
NSLog(@"f = %f and result = %f", f, result);
NSNumber *aFloatNumber = [NSNumber numberWithFloat:[aFloatValue floatValue]];
NSLog(@"aFloatNumber = %@", aFloatNumber);
Format a Number
NSNumber *numberToFormat = [NSNumber numberWithFloat:9.99];
NSLog(@"numberToFormat = %@", numberToFormat);
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
numberFormatter.numberStyle = NSNumberFormatterCurrencyStyle;
NSLog(@"Formatted for currency: %@", [numberFormatter stringFromNumber:numberToFormat]);
numberFormatter.numberStyle = NSNumberFormatterSpellOutStyle;
NSLog(@"Formatted for spelling out: %@", [numberFormatter stringFromNumber:numberToFormat]);
we tried to cover as many as Objective-C data types, still we missed something, please let us know
Happy Coding 🙂
Discover more from CODE t!ps
Subscribe to get the latest posts sent to your email.