Cocoa, Makin’ it Simple.

your source for mac and iphone cocoa code samples

New Cocoapi site! http://www.cocoapi.com

leave a comment »

Written by Shai Wininger

October 18, 2009 at 7:09 am

Posted in Uncategorized

How to convert NSData to NSString and back

leave a comment »


NSString* myString;
myString = [[NSString alloc] initWithData:nsdata encoding:NSASCIIStringEncoding];


NSData* nsdata;
nsdata = [myString dataUsingEncoding: NSASCIIStringEncoding];

Written by Shai Wininger

October 17, 2009 at 7:45 pm

How to get the class type of an object?

leave a comment »

Say you have a class of type “Car” and you need to check if an object you have at runtime is of that type:

if ([myRuntimeObject isKindOfClass:[Car class]]){
//do something....
}

Written by Shai Wininger

September 17, 2009 at 8:36 am

iPhone : How to add a delete button to a UITableView cell

leave a comment »

1. Make sure you have a proper UITableView in place and rigged to the data source and delegate.

2. In the delegate class, add the following code :

- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}

- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath

{
// remove the item from your data
[myItems removeObjectAtIndex:indexPath.row];

// refresh the table view
[tableView reloadData];
}

For all you Jewish Folks: Kotel Notes iPhone App Released!

leave a comment »

iPhone Kotel Notes Application

iPhone Kotel Notes Application

The application, “Kotel Notes”, provides a cool way to send free wishes and prayers, and have them printed and placed (for real) within the Western Wall Stones in Jerusalem.

Use it before an important exam, deal or any other event which requires some spiritual assistance…

Kotel Notes Available on the Apple App Store 

Written by Shai Wininger

June 7, 2009 at 5:44 am

iPhone : famous crash – “this class is not key value coding-compliant for the key”…

with one comment

If you came across this error :

*** Terminating app due to uncaught exception ‘NSUnknownKeyException’, reason: ‘[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key myLabel.’

(replace myLabel with any other name you may have used)

In most cases the cause for this error is a renaming of a UI property such as  an IBoutlet in Xcode without updating the binding in Interface Builder. When you come across this thing, do the following:

  1. Look for the controller that holds your property (in the case of the sample above : myLabel)
  2. Open the controller’s corresponding Nib file in Interface Builder
  3. Reconnect your UI control (in the case of the sample : a UILabel) to the new property name.

Written by Shai Wininger

April 19, 2009 at 7:28 am

iPhone : warning: no ‘-renderInContext:’ method found – solution

with one comment

There is a simple solution to this pesky warning:

  1. Add the following to your .h file :
    #import <QuartzCore/QuartzCore.h>
  2. You may also need to add  “CoreGraphics.framework” to your project, if its not already there.

Written by Shai Wininger

April 12, 2009 at 8:05 am

iPhone : warning: no ‘-renderinInContext’ method found

with one comment

If you are trying to do this:

[myView.layer renderInContext:myCtx];

and get the this warning :

warning: no '-renderinInContext' method found

Simply add the following to the top of your .h file:
#import <QuartzCore/QuartzCore.h>

iPhone : Simple method for converting radians to degrees

leave a comment »

This is a simple method that you can use to throw degrees in and get radians out.


// put this static method somewhere convenient

+ (float)
degreesToRadians:(float)degrees{
return degrees / 57.2958;
}


Written by Shai Wininger

March 23, 2009 at 9:42 am

iPhone : How to rotate an image

leave a comment »

Say you have an UIImageView called : myImage, and you want to rotate it by 35 deg around its center point.

This can be achieved like so:


// first we set the rotation axis. (This is relevant for all transformation actions)
myImage.center = CGPointMake(
0, 0);
// then we apply the transform. Quartz uses radians, so we need to pass radian value and not degrees.
// check out my next post on converting radians to degrees...
myImage.transform = CGAffineTransformMakeRotation([degreesToRadians:imageRotationFix]);

Written by Shai Wininger

March 23, 2009 at 9:37 am

Follow

Get every new post delivered to your Inbox.