Friday, March 18, 2011

Objective C: Logged User Singleton


//loggedUserDataSIngleton.h

#import <Foundation/Foundation.h>

@interface loggedUserDataSIngleton : NSObject {
    NSString *loginName;
}
   
@property (nonatomic, retain) NSString* loginName;
   
+(loggedUserDataSIngleton*)sharedMySingleton;
-(NSString*)obtainCurrentUserLoginName;

@end


//loggedUserDataSIngleton.m

#import "loggedUserDataSIngleton.h"


@implementation loggedUserDataSIngleton

@synthesize loginName;

static loggedUserDataSIngleton *_sharedMySingleton = nil;

+(loggedUserDataSIngleton*)sharedMySingleton
{
    @synchronized([loggedUserDataSIngleton class])
    {
        if (!_sharedMySingleton) {
            [[self alloc] init];
        }
       
        return _sharedMySingleton;
    }
   
    return nil;
}

+(id)alloc
{
    @synchronized([loggedUserDataSIngleton class])
    {
        NSAssert(_sharedMySingleton == nil, @"Attempted to allocate a second instance of a singleton.");
        _sharedMySingleton = [super alloc];
        return _sharedMySingleton;
    }
   
    return nil;
}

-(id)init {
    self = [super init];
    if (self != nil) {
        // initialize stuff here
    }
    return self;
}

-(NSString*)obtainCurrentUserLoginName {
    return self.loginName;
}

@end


then, in the .m you want to use the singleton, you have to import the loggedUserDataSIngleton.h:

#import "loggedUserDataSIngleton.h" 
....
....
....
....
// store login Name in singleton class  
//[loggedUserDataSIngleton sharedMySingleton].loginName = @"John";


// get stored login name
[[loggedUserDataSIngleton sharedMySingleton] loginName]




Reference:  http://getsetgames.com/2009/08/30/the-objective-c-singleton/

create person ABRecordRef: adding a contact to address book



CFErrorRef error = NULL;

ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();


ABRecordRef newPerson = ABPersonCreate();

ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"John", &error);

ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Doe", &error);

Here is the whole guide, it's very complete:


Source: http://www.modelmetrics.com/tomgersic/iphone-programming-adding-a-contact-to-the-iphone-address-book/

Wednesday, March 16, 2011

ASCII url encoding Objective C

 
NSString* escapedUrlString = [unescapedString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

Source: http://mesh.typepad.com/blog/2007/10/url-encoding-wi.html

Tuesday, March 15, 2011

getting value property from ABRecordRef - Extract Address Book Address Values Tutorial

I was searching for the way to get value properies from an ABRecordRef Object and I found out it was using ABRecordCopyValue:


NSString *name = (NSString *)ABRecordCopyValue(person
, kABPersonFirstNameProperty);
 
but the great thing I found was an Amazing Tutorial : 
Tutorial: Extract Address Book Address Values on iPhone OS 

Monday, March 7, 2011

MySQL - OperationalError: Error on rename (errno: 150)

Trying to execute:

alter table `bismark`.`d_area` drop index `tipo_area_id`

You Get:

Error on rename of '.\bismark\#sql-93c_1' to '.\bismark\d_area' (errno: 150)

You have to delete first the foreign key.

ALTER TABLE d_area DROP FOREIGN KEY tipo_area_id_FK;


Then you can drop the index and the column :D


Tuesday, March 1, 2011

.profile does not exist. Setting path var mac os

 - open a terminal console
- go to your User home:
cd /Users/<MyUser>
- create .profile file
touch .profile 
- open .profile file
open .profile
add the export sentence that you need, i.e.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH