Friday, May 31, 2013

NSArray firstObjectCommonWithArray example in Objective C (iOS).


NSArray firstObjectCommonWithArray

Returns the first object contained in the receiving array that’s equal to an object in another given array.

- (id)firstObjectCommonWithArray:(NSArray *)otherArray

Parameters
otherArray
An array.

Return Value
Returns the first object contained in the receiving array that’s equal to an object in otherArray. If no such object is found, returns nil.

Discussion of [NSArray firstObjectCommonWithArray]
This method uses isEqual: to check for object equality.

NSArray firstObjectCommonWithArray example.
- (BOOL)performDragOperation:(id )sender
{
    NSPasteboard *thePasteboard = [sender draggingPasteboard];
    NSArray *types = [thePasteboard types];
    BOOL result = ([types firstObjectCommonWithArray:[self pboardTypes]]
!= nil);
    NSLog(@"performDragOperation %d", result);
    return result;
}

Example of [NSArray firstObjectCommonWithArray].
//use like this
//iOS 5
NSArray *changeRoutes = [NSArray arrayWithObjects:
(NSString*)kAudioSessionOutputRoute_BuiltInReceiver,
(NSString*)kAudioSessionOutputRoute_BuiltInSpeaker,
nil];

if ([[self outputRoutes] firstObjectCommonWithArray:changeRoutes]) { //returns nil if none found}
//**or**//
if([[self outputRoutes] containsObject:(NSString*)kAudioSessionOutputRoute_BuiltInReceiver]){}

//iOS 4
NSArray *changeRoutes = [NSArray arrayWithObjects:
@"ReceiverAndMicrophone",
@"SpeakerAndMicrophone",
nil];

if ([[self outputRoutes] firstObjectCommonWithArray:changeRoutes]) { //returns nil if none found}
//**or**//
if([[self outputRoutes] containsObject:@"ReceiverAndMicrophone"]){}


End of NSArray firstObjectCommonWithArray example article.