Tuesday, 14 May 2013

Code For NSPredicate in objective c(IOS)


    Add a new file(NSObject) in your working application and add following code first..

.h

-(id)initWithName:(NSString *) name;
-(NSString *)description;


.m

@synthesize strName;

-(id)initWithName:(NSString *) name
{
    self = [super init];
    if(self)
    {
        self.strName = [name copy];
        
        
    }
    return self;
}

-(NSString *)description
{
    return self.strName;
}

After that import this file in your desired view and add Following piece of code..




    Employee *emp = [[Employee alloc]initWithName:@"Amit"];
    Employee *emp1 = [[Employee alloc]initWithName:@"Sudheer"];
    Employee *emp3 = [[Employee alloc]initWithName:@"Kiran"];
    
    NSMutableArray *arr1  = [[NSMutableArray alloc]initWithObjects:emp,emp1,emp3 ,nil];
    
    
    NSPredicate *nsPred1 = [NSPredicate predicateWithFormat:@"strName == 'Kiran'"];
    NSPredicate *nsPred2 = [NSPredicate predicateWithFormat:@"strName.length <5"];
    NSPredicate *nsPred3 = [NSPredicate predicateWithFormat:@"strName BEGINSWITH[c] 'a'"];
    NSPredicate *nsPred4 = [NSPredicate predicateWithFormat:@"strName ENDSWITH[c] 'r'"];
    
    NSArray *arrFilterdArray1 = [arr1 filteredArrayUsingPredicate:nsPred1];
    NSArray *arrFilterdArray2 = [arr1 filteredArrayUsingPredicate:nsPred2];
    NSArray *arrFilterdArray3 = [arr1 filteredArrayUsingPredicate:nsPred3];
    NSArray *arrFilterdArray4 = [arr1 filteredArrayUsingPredicate:nsPred4];
    NSLog(@"%@",arrFilterdArray1);
    NSLog(@"%@",arrFilterdArray2);
    NSLog(@"%@",arrFilterdArray3);
    NSLog(@"%@",arrFilterdArray4);


Out Put


2013-05-15 11:50:26.918 StoryBoardApplication[390:207] (
    Kiran
)
2013-05-15 11:50:28.037 StoryBoardApplication[390:207] (
    Amit
)
2013-05-15 11:50:28.661 StoryBoardApplication[390:207] (
    Amit
)
2013-05-15 11:50:29.276 StoryBoardApplication[390:207] (
    Sudheer
)

Tuesday, 7 May 2013

What's the difference between frame and bounds in IOS



The frame  shows  the origin and size of a view in superview coordinates.
The bounds Shows the origin in the view’s coordinates and its size (the view’s content may be larger than the bounds size).




 NSLog(@"%@",NSStringFromCGRect(self.btnNext.frame));
 NSLog(@"%@",NSStringFromCGRect(self.btnNext.bounds));

OutPut :

frame :
2013-05-07 15:48:48.998 FirstStoryBoardViewController[1825:c07] {{51, 153}, {181, 44}}
Internal error [IRForTarget]: Couldn't rewrite external variable _ZZ52-[KMGViewController($__lldb_category) $__lldb_expr:]E19$__lldb_expr_result

Bounds :
2013-05-07 15:49:00.941 FirstStoryBoardViewController[1825:c07] {{0, 0}, {181, 44}}
Internal error [IRForTarget]: Couldn't rewrite external variable _ZZ52-[KMGViewController($__lldb_category) $__lldb_expr:]E19$__lldb_expr_result

Wednesday, 1 May 2013

Difference between NSArray and NSMutableArray in Objective C


      NSArray is Immutable and ordered collection i.e you can not update if once its  initialized while
    NSMutableArray  is Mutable and ordered collection  i.e you can add more object as per your requirement     



Example:

    NSArray *arr1 = [[NSArray alloc]initWithObjects:@"Amit",@"Jai",@"Sudheer"nil];
        NSLog(@"%@",arr1);
outPut :

2013-05-02 11:34:43.372 FirstStoryBoardViewController[418:c07] (
    Amit,
    Jai,
    Sudheer
)

[arr1 addObject:@"Kiran"]; if we try to do like this,we get build error as 


    No visible @interface for 'NSArray' declares the selector 'addObject':

   NSMutableArray *Muarr1 = [[NSMutableArray alloc]initWithObjects:@"Amit",@"Jai",@"Sudheer"nil];
  NSLog(@"%@",Muarr1);

outPut :

2013-05-02 11:34:46.226 FirstStoryBoardViewController[418:c07] (
    Amit,
    Jai,
    Sudheer
)



 [Muarr1 addObject:@"Kiran"];
NSLog(@"%@",Muarr1);
 outPut :

  2013-05-02 11:37:37.178 FirstStoryBoardViewController[458:c07] (
    Amit,
    Jai,
    Sudheer,
    Kiran
)


      

Differences Between NSArray And NSSet in Objective C



NSSet
  • Primarily access items by comparison
  • Unordered
  • Does not allow duplicates
NSArray
  • Can access items by index
  • Ordered
  • Allows duplicates
Example


    NSSet *set = [NSSet setWithObjects:@"Amit",@"Rakesh",@"Kiran",@"Jai",@"zebra",@"Amit",@"Kiran"nil];
    NSArray *arr = [[NSArray alloc]initWithObjects:@"Amit",@"Rakesh",@"Kiran",@"Jai",@"zebra",@"Amit",@"Kiran"nil];
    
    NSLog(@"%@",set);
    NSLog(@"%@",arr);


outPut

2013-05-01 11:45:53.881 FirstStoryBoardViewController[630:c07] {(
    Jai,
    zebra,
    Rakesh,
    Amit,
    Kiran
)}
2013-05-01 11:45:54.384 FirstStoryBoardViewController[630:c07] (
    Amit,
    Rakesh,
    Kiran,
    Jai,
    zebra,
    Amit,
    Kiran
)