Monday, 20 May 2013

Get The Max Value from an NSArray



-(void)findMax
{
    NSArray *numberArray = [NSArray arrayWithObjects:[NSNumber numberWithInt:10], [NSNumber numberWithInt:20], [NSNumber numberWithInt:1000], nil];
    NSInteger highestNumber;
    NSInteger numberIndex;
    for (NSNumber *theNumber in numberArray)
    {
        if ([theNumber integerValue] > highestNumber) {
            highestNumber = [theNumber integerValue];
            numberIndex = [numberArray indexOfObject:theNumber];
        }
    }
    NSLog(@"Highest number: %i at index: %i", highestNumber, numberIndex);
}


Out Put :

Highest number: 1000 at index: 2


Using KVC


[self setValue:[NSNumber numberWithInt:10] forKey:@"number1"];
[self setValue:[NSNumber numberWithInt:20] forKey:@"number2"];
[self setValue:[NSNumber numberWithInt:1000] forKey:@"number3"];
   
NSArray *numberArray = [NSArray arrayWithObjects:[self valueForKey:@"number1"], [self valueForKey:@"number2"], [self valueForKey:@"number3"], nil];
    
    
    int max = [[numberArray valueForKeyPath:@"@max.intValue"] intValue];
    
   NSLog(@"Highest number: %i",max);

Out Put :




Highest number: 1000

Thursday, 16 May 2013

Using NSMutableString print the Triangle


-(void)triAngle
{
    NSMutableString *strTriangle = [[NSMutableString alloc]init];
    for (int i = 0; i<3; i++)
    {
        for (int j = 0; j<=i; j++)
        {
            if(j==0 && i==0)
            {
                [strTriangle setString:@"\n*"];
            }
            else
             [strTriangle appendString: @"*"];
            
        }
      [strTriangle appendString: @"\n"];
    }
    NSLog(@"%@",strTriangle);
    
}

OutPut :

*
**
***

Steps to show the hidden files on your mac

1. Launch the Terminal(/Application/Utilities)

OR

Hold the command key and press the space bar you could see the search box right corner of your mac and type terminal.

Click on spotlight search icon  right corner of your mac and type terminal.

2. Type the following command exactly

defaults write com.apple.Finder AppleShowAllFiles TRUE

then for relaunch the finder Type the following command exactly..

killall Finder 

Wednesday, 15 May 2013

What is KVC and KVO



------------------KVC--------------
Character.h

#import <Foundation/Foundation.h>

@interface Character : NSObject
{
    NSString *Name;
    NSInteger Rating;
}
@property (nonatomic, copy) NSString *Name;
@property(nonatomic, assign) NSInteger Rating;

@end



Character.m
#import "Character.h"

@implementation Character
@synthesize Name;
@synthesize Rating;

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"required"])
    {
        //NSLog(@"%@",[object valueForKeyPath:keyPath]);
         NSLog(@"%@",[change  objectForKey:NSKeyValueChangeOldKey]);
    }
}

@end




-(void)KVC
{

    Character *conharector1;    
    // KVC...
    conharector1 = [[Character alloc] init];
    [conharector1 setValue:@"Amit" forKey:@"Name"];
    [conharector1 setValue:[NSNumber numberWithInt:10] forKey:@"Rating"];
    
    //Done! 
    
    NSString *GetconharectorName = [conharector1 valueForKey:@"Name"];
    NSNumber *GetconharectorRating = [conharector1 valueForKey:@"Rating"];
    
    NSLog(@"%@ Rating = %d  ", GetconharectorName, [GetconharectorRating intValue]);
    
}


OUTPUT :


2013-05-16 10:58:20.263 StoryBoardApplication[536:c07] Amit Rating = 10  


----------------------- KVO---------------

Employee.h
#import <Foundation/Foundation.h>
@interface Employee : NSObject
{
    int required;
}
@property(nonatomic,assign) int required;

-(id)init;
@end







Employee.m

#import "Employee.h"

@implementation Employee
@synthesize required;
-(id)init
{
     self = [super init];
    if(self)
    {
        required = 1;
    }
    return self;
}

@end



Manager.h

import <Foundation/Foundation.h>

@interface Manager : NSObject

@end

Manager.m


#import "Manager.h"

@implementation Manager
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"required"])
    {
        //NSLog(@"%@",[object valueForKeyPath:keyPath]);
        NSLog(@"%@",[change  objectForKey:NSKeyValueChangeNewKey]);
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Changed" message:@"Required value has changed By Employee" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }
}
@end



Following method you can call whereever you want to :

-(void)KVO
{
    Employee *employee = [[Employee  alloc]init];
    Manager *manager = [[Manager alloc]init];
    
    [employee addObserver:manager forKeyPath:@"required" options:NSKeyValueObservingOptionNew   context:NULL];
    [employee setRequired:67];
    [employee removeObserver:manager forKeyPath:@"required"];
    

OUTPUT :

2013-05-16 10:56:25.601 StoryBoardApplication[512:c07] 67

and an alert like : Required value has changed By Employee

I hope it will be help you....





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
)