Skip to content

Posts from the ‘Objective C Tips’ Category

14
Nov

‘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];
10
Nov

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.

https://itunes.apple.com/us/app/nuwbii/id1058372255

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);
2
Nov

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");
}
2
Nov

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.

2
Nov

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);
14
Oct

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);
8
Feb

Tip: Hide top status bar of iPhone

To hide your top iPhone status bar use:
[objc]
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
[/objc]

in your Objective C code (for example in loadView method, if you are using view based application).

11
Dec

Copy a NSString to a new string

To copy a NSString to a new string use the following code:

NSString *str1 = @"This is a string";
NSString *str2;
str2 = [NSString stringWithFormat:@"%@", str1];