Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of category methods do you use to make Cocoa programming easier?

I use a collection of category methods for Cocoa's built in classes to make my life easier. I'll post some examples, but I really want to see what other coders have come up with. What kind of handy category methods are you using?

Example #1:

@implementation NSColor (MyCategories)
+ (NSColor *)colorWithCode:(long)code
{
    return [NSColor colorWithCalibratedRed:((code & 0xFF000000) >> 24) / 255.0
                                     green:((code & 0x00FF0000) >> 16) / 255.0
                                      blue:((code & 0x0000FF00) >>  8) / 255.0
                                     alpha:((code & 0x000000FF)      ) / 255.0];
}
@end

// usage:
NSColor * someColor = [NSColor colorWithCode:0xABCDEFFF];

Example #2:

@implementation NSView (MyCategories)
- (id)addNewSubViewOfType:(Class)viewType inFrame:(NSRect)frame
{
    id newView = [[viewType alloc] initWithFrame:frame];
    [self addSubview:newView];
    return [newView autorelease];
}
@end

// usage:
NSButton * myButton = [someView addNewSubviewOfType:[NSButton class]
                                            inFrame:someRect];
like image 311
e.James Avatar asked Aug 27 '09 16:08

e.James


People also ask

What language is cocoa from?

Etymology 1. From Spanish cacao, from Classical Nahuatl cacahuatl. The form cocoa by confusion with coco, popularized by Samuel Johnson's A Dictionary of the English Language. Doublet of cacao.

What is the difference between Cocoa and Cocoa Touch?

Cocoa, which includes the Foundation and AppKit frameworks, is used for developing applications that run on OS X. Cocoa Touch, which includes Foundation and UIKit frameworks, is used for developing applications that run on iOS.

What is Cocoa Touch framework in iOS?

Cocoa Touch is the application development environment for building software programs to run on iOS for the iPhone and iPod Touch, iPadOS for the iPad, watchOS for the Apple Watch, and tvOS for the Apple TV, from Apple Inc. Cocoa Touch. Developer(s) Apple Inc. Operating system.

Is cocoa an open source?

There are also open source implementations of major parts of the Cocoa framework, such as GNUstep and Cocotron, which allow cross-platform Cocoa application development to target other operating systems, such as Microsoft Windows and Linux.


3 Answers

I've really been loving Andy Matuschak's "KVO+Blocks" category on NSObject. (Yes, it adds some new classes internally as implementation details, but the end result is just a category on NSObject). It lets you provide a block to be executed when a KVO-conforming value changes rather than having to handle every KVO observation in the observeValueForKeyPath:ofObject:change:context: method.

like image 124
Matt Ball Avatar answered Sep 23 '22 10:09

Matt Ball


Regular Expressions with RegexKitLite. Download @ RegexKitLite-3.1.tar.bz2

like image 36
johne Avatar answered Sep 25 '22 10:09

johne


Category, which adds md5/sha1 hashing to NSString. NSData one is similar.

#define COMMON_DIGEST_FOR_OPENSSL
#import <CommonCrypto/CommonDigest.h>


@implementation NSString(GNExtensions)

    - (NSString*)
    hashMD5
    {
        NSData* data = [self dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion: NO];

        unsigned char hashingBuffer[16];
        char outputBuffer[32];

        CC_MD5([data bytes], [data length], hashingBuffer);

        for(int index = 0; index < 16; index++)
        {
            sprintf(&outputBuffer[2 * index], "%02x", hashingBuffer[index]);
        }

        return([NSString stringWithCString: outputBuffer length: 32]);
    }


    - (NSString*)
    hashSHA1
    {
        NSData* data = [self dataUsingEncoding: NSUTF8StringEncoding allowLossyConversion: NO];

        unsigned char hashingBuffer[20];
        char outputBuffer[40];

        CC_SHA1([data bytes], [data length], hashingBuffer);

        for(int index = 0; index < 20; index++)
        { 
            sprintf(&outputBuffer[2 * index], "%02x", hashingBuffer[index]);
        }

        return([NSString stringWithCString: outputBuffer length: 40]);
    }


@end
like image 33
ttvd Avatar answered Sep 26 '22 10:09

ttvd