»
S
I
D
E
B
A
R
«
Sort an NSMutableArray with custom objects
Dec 6th, 2009 by admin

To sort an NSMutableArray with custom objects, you can either implement a compare-method for your object:

- (NSComparisonResult)compare:(id)otherObject {
    return [self.birthDate compare:otherObject];
}

NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)];

or even better: (The default sorting selector of NSSortDescriptor is compare:)

NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"birthDate"
                                              ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];
int myInt = (int) myFloat;
Programmatically send SMS on the iPhone
Nov 28th, 2009 by admin

To send SMS on the iPhone programmatically, You can use a sms:[target phone number] URL to open the SMS application.

instanceof in Objective-C
Nov 28th, 2009 by admin

There is no instanceof in Objective-C, but there are ways to create the same result:

1. [myObject class] for returning the class of an object.

You can make exact comparisons with:

if ([myObject class] == [MyClass class])

but not by using directly MyClass identifier.

2. Similarily, you can find if the object is of a subclass of your class with:

if ([myObject isKindOfClass:[AnObject class]])

Global Constants in Objective-C
Nov 28th, 2009 by admin

A good way to make constants defined once for the whole app is to create a header file like:

// Constants.h
extern NSString * const MyFirstConstant;
extern NSString * const MySecondConstant;
//etc.

You can include this file in each file that uses the constants or in the pre-compiled header for the project.

You define these constants in a .m file like

// Constants.m
NSString * const MyFirstConstant = @"FirstConstant";
NSString * const MySecondConstant = @"SecondConstant";

Constants.m should be added to your application/framework’s target so that it is linked in to the final product.

The advantage of using string constants instead of #define’d constants is that you can test for equality using pointer comparison (stringInstance == MyFirstConstant) which is much faster than string comparison ([stringInstance isEqualToString:MyFirstConstant]) (and easier to read, IMO).

Base64 library on iPhone SDK
Nov 28th, 2009 by admin

If you’d like to do base64 encoding and decoding, but could not find any support from iPhone SDK, don’t worry. Here’s a nice code sample at the bottom of this post. Very self-contained…

http://www.cocoadev.com/index.pl?BaseSixtyFour

Defining a preprocessor symbol in Xcode
Nov 28th, 2009 by admin

To define a preprocessor symbol in Xcode, go to your Target or Project settings, click the Gear icon at the bottom left, and select “Add User-Defined Setting”. The new setting name should be GCC_PREPROCESSOR_DEFINITIONS, and you can type your definitions in the right-hand field.

the full syntax is:

constant_1=VALUE constant_2=VALUE

Note that you don’t need the ‘=’s if you just want to #define a symbol, rather than giving it a value (for #ifdef statements)

Converting a float to an int in Objective-C
Nov 28th, 2009 by admin

To convert a float to an int in Objective-C is not that much different from the classic C-style, C-style casting syntax works in Objective C, so try this:

int myInt = (int) myFloat;
Remove from NSMutableArray while Iterating
Nov 28th, 2009 by admin

Here’s a sample using Objective-C 2.0 syntax to remove array items from NSMutableArray while iterating:

NSMutableArray *discardedItems = [NSMutableArray array];
SomeObjectClass *item;

for (item in originalArrayOfItems) {
    if ([item shouldBeDiscarded])
        [discardedItems addObject:item];
}

[originalArrayOfItems removeObjectsInArray:discardedItems];
Wrapping Text in UITableViewCell without Custom Cell
Nov 28th, 2009 by admin

Inside your cellForRowAtIndexPath …. function. The first time you create your cell:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.numberOfLines = 0;
        cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
}

You’ll notice that the number of lines were set for the label to 0. This lets it use as many lines as it needs.

The next part is to specify how large your UITableViewCell will be, so do that in your heightForRowAtIndexPath function:
[/sourcecode]
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellText = @”Go get some text for your cell.”;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:17.0];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];

return labelSize.height + 20;
}
[/sourcecode

Generating Random Numbers in Objective-C
Nov 28th, 2009 by admin

Same as C, in Objective-C, to generate random numbers, the following example includes 0 but excludes 74:

#include <stdlib.h>
...
int r = rand() % 74

(assuming you meant including 0 but excluding 74, which is what Java does)

Feel free to substitute random() or arc4random() for rand() (which is, as others have pointed out, quite sucky).

»  Substance: WordPress   »  Style: Ahren Ahimsa