Skip to content

Instantly share code, notes, and snippets.

View valexa's full-sized avatar
💼
Open for work

Vlad Alexa valexa

💼
Open for work
View GitHub Profile
-(NSString*)execTask:(NSString*)launch args:(NSArray*)args
{
if ([[NSFileManager defaultManager] isExecutableFileAtPath:launch] != YES){
[bottomLeftLabel setStringValue:[NSString stringWithFormat:@"%@ not found",launch]];
return nil;
}
NSPipe *stdout_pipe = [[NSPipe alloc] init];
* thread #1: tid = 0x1c07, 0x0000000100040767 Memory Magic`-[TestString dealloc] + 23 at TestString.m:29, stop reason = breakpoint 1.1
frame #0: 0x0000000100040767 Memory Magic`-[TestString dealloc] + 23 at TestString.m:29
frame #1: 0x0000000100002a3c Memory Magic`-[MenuBar theEvent:] + 1116 at MenuBar.m:58
frame #2: 0x00007fff85b5547a CoreFoundation`_CFXNotificationPost + 2554
frame #3: 0x00007fff8ad0c846 Foundation`-[NSNotificationCenter postNotificationName:object:userInfo:] + 64
frame #4: 0x00000001000038d8 Memory Magic`-[MenuBarIcon memoryLoop] + 984 at MenuBarIcon.m:53
frame #5: 0x0000000100003a22 Memory Magic`-[MenuBarIcon initWithFrame:] + 210 at MenuBarIcon.m:65
frame #6: 0x00007fff82767148 AppKit`-[NSView init] + 62
frame #7: 0x000000010000255e Memory Magic`-[MenuBar init] + 366 at MenuBar.m:25
frame #8: 0x00007fff826abeca AppKit`-[NSCustomObject nibInstantiate] + 382
@valexa
valexa / gist:1940170
Created February 29, 2012 11:21
conformsToProtocol
int class_count = objc_getClassList(nil, 0);
Class *buffer = calloc(class_count, sizeof(Class));
if (buffer) {
//NSLog(@"allocated a %d bytes buffer",malloc_size(buffer));
objc_getClassList(buffer,class_count);
//NSLog(@"total %i classes",class_count);
for (int i = 0; i < class_count; ++i) {
Class currClass = buffer[i];
if ( class_respondsToSelector(currClass, @selector(conformsToProtocol:)) ) {
if ([currClass conformsToProtocol:@protocol(NSMutableCopying)]) {
@valexa
valexa / gist:1809438
Created February 12, 2012 16:23
nsstrings
NSString *foo = @"foo";
NSString *bar = [NSString stringWithString:@"bar"];
NSString *baz = [[NSString alloc] initWithString:@"baz"];
NSMutableString *bax = [NSMutableString stringWithString:@"bax"];
NSData *objData = [NSData dataWithBytes:foo length:malloc_size(foo)];
NSLog(@"%s foo contains %@, hash %lu, retain %i",class_getName(object_getClass(foo)),objData,[foo hash],[foo retainCount]);
objData = [NSData dataWithBytes:bar length:malloc_size(bar)];
NSLog(@"%s bar contains %@, hash %lu, retain %i",class_getName(object_getClass(bar)),objData,[bar hash],[bar retainCount]);
@valexa
valexa / gist:1808794
Created February 12, 2012 14:29
self accessor gotcha
@interface AppDelegate : NSObject <NSApplicationDelegate> {
NSMutableString *retainedProperty; //explicitly declaring ivar backing is only required for the 32 bit runtime, is automatically added by the compiler for 64 bit
}
@property (retain) NSMutableString *retainedProperty;
@end
@implementation AppDelegate
@valexa
valexa / hash or isEqual issue
Created December 17, 2011 14:59
changing [ret description] to ret does not work as expected
NSMutableArray *ivarPrevious = [NSMutableArray arrayWithCapacity:1];
-(void)foo
{
NSMutableArray *newivarPrevious = [NSMutableArray arrayWithCapacity:1];
for (...){
NSMutableDictionary *ret = [NSMutableDictionary dictionaryWithCapacity:1];
[ret setObject:foo forKey:@"foo"];
[ret setObject:bar forKey:@"bar"];
[newivarPrevious addObject:[ret description]];
@valexa
valexa / gist:1273279
Created October 9, 2011 04:12
1 to 1 mapping bug
#import "DetailsDataSource.h"
@implementation DetailsDataSource
@synthesize rootItems;
- (id)init
{
self = [super init];
@valexa
valexa / editNestedDict
Created April 18, 2011 13:42
a method for changing objects inside deeply nested dictionaries
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
//create a example nested dictionary with just 4 levels
NSMutableDictionary *parent = [[[NSMutableDictionary alloc] init] autorelease];
NSDictionary *thirdChild = [NSDictionary dictionaryWithObjectsAndKeys:@"bi",@"l4",@"nar",@"l4_", nil];
NSDictionary *secondChild = [NSDictionary dictionaryWithObjectsAndKeys:thirdChild,@"l3",@"bar",@"l3_", nil];
NSDictionary *firstChild = [NSDictionary dictionaryWithObjectsAndKeys:secondChild,@"l2",secondChild,@"l2_", nil];
[parent setObject:firstChild forKey:@"l1"];
[parent setObject:firstChild forKey:@"l1_"];
@interface VAImage : UIView <VAUrlConnectionDelegate> {
VAUrlConnection *conn;
}
- (void) connectionDidFinish:(VAUrlConnection *)theConnection;
@implementation VAImage
NSArray *sortedFirstNames = [firstNames sortedArrayUsingFunction:familyNameCompare context:NULL];
NSInteger familyNameCompare(NSString *one, NSString *two, void *context)
{
NSArray *sortedFamilyNames = [[FirstNamesAndFamilyNames allValues] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSString *oneName = [FirstNamesAndFamilyNames objectForKey:one];
NSString *twoName = [FirstNamesAndFamilyNames objectForKey:two];