Skip to content

Posts from the ‘Objective C’ Category


Switch Between Header and Implementation File in Xcode

If you want to switch between header file and implementation file in Xcode, there’s a shortcut for that. Just press:

Ctrl + Cmd + Up Arrow or Down Arrow


Fix SDL2 Crash When Compiling using Xcode on Mac OSX

If you want your SDL2 app to compile without crashing you must sign it. In order to do that let’s go to SDL2.framework directory:

cd /Applications/ MacOSX.platform/

To check if SDL2 is code signed run:

codesign -vvvv SDL2

To signe SDL2 run:

codesign -f -s - SDL2

Note: If you are using an older version of OSX then the path where SDL2.framework is located might be different. For example on OSX 10.7 we can have an Xcode that has SDK for OSX 10.8 located in: /Applications/
Developer /SDKs/MacOSX10.8.sdk/System/Library/Frameworks/SDL2.framework/


Find out details about an image with ImageMagick utility

We can find out details about an image using identify tool from ImageMagick package, available on multiple OSes:

identify -verbose img1.bmp

‘initWithCString:’ is deprecated: first deprecated in iOS 2.0

Let’s say we got the following warning when trying to compile an Objective C code:

‘initWithCString:’ is deprecated: first deprecated in iOS 2.0

The line is:

NSString* nsString = [[NSString alloc] initWithCString: fileNameWE];

To solve the warning we will use stringWithFormat method instead so our modified code is:

NSString* nsString =[NSString stringWithFormat:@"%s", fileNameWE];

Play With Arrays in Objective C

Next example show you how to create an array in objective C, how to iterate through the array and how to add or remove elements from the array.

NSArray *anArray = @[@"element1", @"element2", @"element3"];
// iterate through the array
for (NSString *aString in anArray) {
    NSLog(@"%@", aString);
// adding elements to an array
NSMutableArray *anotherArray = [[NSMutableArray alloc] init];
[anotherArray addObject:@"element10"];
[anotherArray addObject:@"element11"];
[anotherArray addObject:@"element12"];
for (NSString *aString in anotherArray) {
    NSLog(@"%@", aString);
// insert object at a specified position
[anotherArray insertObject:@"elem22" atIndex:1];
for (NSString *aString in anotherArray) {
    NSLog(@"%@", aString);
// remove element from a specified position
[anotherArray removeObjectAtIndex:3];
// remove last object
[anotherArray removeLastObject];
// find number of records in a NSArray or NSMutableArray
NSInteger noOfRows = (NSInteger)[anotherArray count];
NSLog(@"Number of rows in the array %ld", noOfRows);

Compare Two Strings in Objective C

Next example shows how to compare two strings in Objective C. For that purpose we’ll use isEqualTo method.

NSString *aString = @"aaa";
NSString *anotherString = @"aaa";
if ( [aString isEqualTo:anotherString] ) {
     NSLog(@"Strings are equal");
} else {
     NSLog(@"Strings are not equal");

Convert from string to integer in Objective C

Next example shows you how to convert a string to integer value.

NSString *aString = @"200";
int anInt = (int)[aString integerValue];
NSLog(@"anInt = %d", anInt);

Note that we use cast to int (int) otherwise we get a warning: “Implicit conversion loses integer precision: ‘NSInteger’ (aka ‘long’) to ‘int’“. Instead of cast to int we could define anInt as long.


Convert from integer to string in Objective C

Next example shows you how to convert from integer to string in Objective C using stringWithFormat function.

int anInt = 100;
NSString *aString = [NSString stringWithFormat:@"%d", anInt];
NSLog(@"aString = %@", aString);

Work With Numbers in Objective C (NSNumber and NSDigitalNumber)

Next example shows you how to work with NSNumber and NSDecimalNumer objects in Objective C. First part show you standard use, second part shows how to work with NSNumer objects in simplified form and last part shows how to work with NSDecimalNumber objects.

In this example we define two NSNumber numbers and then we add them (or we muliply them).

NSNumber *a, *b, *c;
a = [NSNumber numberWithInt:10];
b = [NSNumber numberWithInt:20];
c = [NSNumber numberWithInt:([a intValue] + [b intValue])];
// short notations
a = @10;
b = @20;
c = @([a integerValue] * [b integerValue]);
// using NSDecimalNumbers
NSDecimalNumber *a1, *a2, *a3;
a1 = [[NSDecimalNumber alloc] initWithInt:15];
a2 = [[NSDecimalNumber alloc] initWithInt:30];
a3 = [a1 decimalNumberByMultiplyingBy:a2];
// define a boolean number
NSNumber *isItTrue = @YES;
// convert string to int
int val1 = [@"100" intValue];
NSNumber *d = [NSNumber numberWithInt:val1];
NSLog(@"\n isItTrue = %@ | d = %@", isItTrue, d);

You can also learn how to convert a NSString to a NSNumber object.

Working with NSNumbers to make computations is slow. Better use C type numbers instead. But anyway just in case you need this, it might be useful to know how to use NSNumber object to make mathematical operations.


Convert Characters of a String to Upper Letters

See next example that shows you how to convert a string to upper case letters in Objective C:

NSString* aString = @"The brown fox jumped over the lazy dog.";
NSString* upperString = [aString uppercaseString];
NSLog(@"%@", upperString);