Monday, 28 March 2016

Collection in iOS (Objective C)

Use of NSSet

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //NSSet Example which uses its own for loop to print seperating by comma.
        NSSet *names=[NSSet setWithObjects:@ "Stefan",@ "Silas",@ "Damon", nil];
        NSLog(@ "%@",names);
        
        //To calculate the number of objects in a set.
        NSLog(@ "The set has %lu elements",names.count);
        
        //Fast Enumeration
        for(id name in names){
            NSLog(@ "%@",name);
        }
        
        //enumerate objects using Block
        [names enumerateObjectsUsingBlock:^(id obj, BOOL *stop){
            NSLog(@ "Current item: %@",obj);
            if ([obj isEqualToString:@ "Damon"]) {
                NSLog(@ "I was looking for Damon and I found him!");
                *stop=YES; //stop enumerating item
            }
        }];
    }
    return 0;

}

No comments:

Post a Comment

Read contacts in iOS using Swift with error handling

Just copy and paste the below code Pre-step before pasting the below code Add below line in AppDelegate.swift file below import statement ...