Showing posts with label Objective-C. Show all posts
Showing posts with label Objective-C. Show all posts

Thursday, March 22, 2012

2 Enums having common values ?

As we mentioned in our previous post, there is this unique problems in enums you may come across sometime that what if 2 enums ended up having some values same, your code will show ERROR, how to solve this ? I will give an example:
Suppose you have an enum

typedef enum
{
        Red ,
        Green,
        Yellow
}BarColors;





typedef enum
{
        Voilet,
        Indigo,
        Blue,
        Green,
        Yellow,
        Orange,
        Red
}RainbowColors;




Now you can see that Red, Green and Yellow are same in both enums. You will get error if you have both enums in your same code. How to deal with it, conventionally you can add a prefix to each enum value like following:

typedef enum
{
        b_Red ,
        b_Green,
        b_Yellow
}BarColors;





typedef enum
{
        r_Voilet,
        r_Indigo,
        r_Blue,
        r_Green,
        r_Yellow,
        r_Orange,
        r_Red
}RainbowColors;


This will solve the error.

Wednesday, June 29, 2011

Integrating C++ files with Objective-C (tips)

This trick will be helpful for people who are trying to embed Opengl ES code in their iPhone project.
One way to let your compiler know that you will be using mix of C++ and objective-C will be to convert the extension of all .m files to .mm as discussed in one the previous post(LINK) but there is another way if you don't want to change all the extensions  !


In your project Settings, Under Build Settings > GCC 4.2- Language
Change the "Compile Source As" value to "Objective-C++"
































It will let you compile the code without changing the extensions of all the files.

I found this wonderful tip from Pete Cole blog. LINK

Thursday, June 16, 2011

Copy items of NSMutableArray into another ?

Lets say you have arrayA and arrayB, arrayB could be NSArray or NSMutableArray.
But arrayA should be mutable.


  [arrayA addObjectsFromArray:arrayB];





Here you are copying items from  arrayB to arrayA. Make sure you have already alloc memory for arrayA as:

arrayA = [[NSMutableArray alloc]init];




Case 2: If you want to create a copy an array to NSArray, you can do so as following:

NSArray *db=[[NSArray alloc]initWithArray:availListArray];



Monday, June 13, 2011

Conversion between NSString and CFStringRef ?

Its a simple typecast conversion !


Convert NSString to CFStringRef


NSString *test = @"Everyone is created equal.";
CFStringRef string =  (CFStringRef) test;


Convert  CFStringRef to NSString

NSString *backToNSString =  (NSString *) string;


Thursday, June 9, 2011

How to get the Date Components from NSDate in Objective-C ?



NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit fromDate:date];
NSInteger _day=[dateComponents day];

dateComponents = [[NSCalendar currentCalendarcomponents:NSMonthCalendarUnit fromDate:date];
NSInteger _month=[dateComponents month];








Similarly, you can get the other components like year, hour, seconds etc , you will have to change components: to relevant CalendarUnit and you can access them with [dateComponents year], [dateComponents hour] etc.

How to use NSDateFormatter in Objective-C ?


       NSDate *date = [NSDate date];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"dd-MM-yyyy"];
    NSString *_date=[_formatter stringFromDate:date];

       NSDate *date = [NSDate date];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"HH.mm.ss"];
    NSString *_time=[_formatter stringFromDate:date];



Given above 2  examples above display how to get current Date in format specified by NSDateFormatter  f and how to get current Time in format specified. You will get the output looking something like 13-05-2011 for the first piece of code(Note that date will be the today's date) and second code is giving out the time as 09.21.33 (hour, mins then followed by seconds).

Convert TimeStamp to NSDate in Objective-C

Here is a code piece which lets you convert the unix type timestamp to Formatted Date


    
    NSString * timeStampString =@"1304245000";
    NSTimeInterval _interval=[timeStampString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *_formatter=[[NSDateFormatter alloc]init];
    [_formatter setDateFormat:@"dd.MM.yyyy"];
    NSString *_date=[_formatter stringFromDate:date];


This will print  _date something like 05.03.2011